namespace Nexus.Api.Models;

public sealed record DashboardAgentInfo(
    string Id,
    string Name,
    string Role,
    string Model,
    bool IsActive,
    string? CurrentTask,
    string? Description,
    string[] Tags,
    double? Progress = null,
    int Workload = 0,
    string? Goal = null,
    string RoleBadge = "badge-slate",
    string StatusLabel = "Bereit",
    string StatusKind = "ready",
    string? StatusDetail = null,
    string? Elapsed = null,
    string? Think = null,
    string? Next = null,
    long? TotalTokens = null,
    decimal? CostUsd = null,
    DateTimeOffset? TelemetryAt = null
);

public sealed record MessageEntry(
    string Role,
    string Content,
    string Timestamp
);

public sealed record ChatRequest(
    string Message,
    string? AgentId
);

public sealed record ChatResponse(
    bool Ok,
    string? Reply,
    string? Error
);

public sealed record FeedEntry(
    string Agent,
    string Action,
    string Timestamp,
    string Time,
    string? AgentId = null,
    string? Type = null
);

public sealed record DashboardStatus(
    bool GatewayOk,
    string IrisStatus,
    int ActiveAgents,
    int PendingTasks
);

public sealed record QueueItem(
    string Id,
    string Name,
    string Status,
    string Priority,
    string Source,
    string WaitTime
);

public sealed record AgentModelInfo(
    string Model,
    string Provider
);

public sealed record SetModelRequest(
    string Model
);

public sealed record ModelOption(
    string Id,
    string Name,
    string Provider
);

// ── Dashboard Task DTOs ──

public sealed record DashboardTaskDto(
    Guid Id,
    string Title,
    string? Detail,
    string Source,
    string State,
    string Priority,
    string? AssignedTo,
    Guid? ParentTaskId,
    DateTimeOffset? DueDate,
    DateTimeOffset CreatedAt,
    DateTimeOffset UpdatedAt,
    bool IsAgentTask = false,
    string? ExpectedFrom = null,
    string? LastActivityMessage = null,
    DateTimeOffset? LastActivityAt = null,
    List<DashboardTaskDto>? ChildTasks = null,
    int ChildTaskCount = 0,
    int OpenChildTaskCount = 0,
    bool HasVisibleDelegation = false,
    Guid? ProjectId = null,
    OperationResultDto? Operation = null
);

public sealed record CreateDashboardTaskRequest(
    string Title,
    string? Detail,
    string? Source,
    string? Priority,
    string? AssignedTo,
    Guid? ParentTaskId = null
);

public sealed record CreateAgentTaskRequest(
    string Title,
    string? Detail,
    string? Source,
    string? Priority,
    string? AssignedTo,
    string? ExpectedFrom,
    Guid? ParentTaskId = null,
    bool StartsInProgress = true,
    string? InitialState = null
);

public sealed record UpdateDashboardTaskRequest(
    string? Title,
    string? Detail,
    string? Source,
    string? Priority,
    string? AssignedTo,
    DateTimeOffset? DueDate = null
);

public sealed record UpdateDashboardTaskStatusRequest(
    string Status
);

public sealed record AgentActivityEntry(
    string Time,
    string Text,
    DateTimeOffset Timestamp,
    string Source = "gateway-session-history"
);

public sealed record GatewayRuntimeInfo(
    bool Reachable,
    string BaseUrl,
    string? Version,
    string? RequiredVersion,
    bool VersionPinned,
    bool VersionMatches,
    string VersionStatus,
    DateTimeOffset CheckedAt,
    string? Message,
    string? Warning = null
);

// ── Task Board DTOs ──

public sealed record BoardResponse(
    List<DashboardTaskDto> Offen,
    List<DashboardTaskDto> InProgress,
    List<DashboardTaskDto> Review,
    List<DashboardTaskDto> Blocked,
    List<DashboardTaskDto> Done
);

public sealed record MoveTaskRequest(
    string State
);

public sealed record ResetStaleRequest(
    int StaleHours = 2
);

public sealed record ResetStaleResponse(
    int ResetCount,
    OperationResultDto? Operation = null
);

public sealed record PostActivityRequest(
    string Message,
    string? Type = null
);

// ── Agent Workflow DTOs ──

/// <summary>
/// Overview of the agent workflow state, grouping tasks by expected respondent
/// and highlighting stale tasks. Used by Iris to see who she is waiting for.
/// </summary>
public sealed record AgentWorkflowOverview(
    List<DashboardTaskDto> WaitingForBao,
    List<DashboardTaskDto> WaitingForIris,
    List<DashboardTaskDto> WaitingForOthers,
    List<DashboardTaskDto> StaleTasks,
    TimeSpan StaleThreshold
);

// ── Notification DTOs ──

public sealed record NotificationDto(
    Guid Id, string Type, string Title, string? Message,
    string ForUser, Guid? TaskId, bool IsRead, DateTimeOffset CreatedAt,
    OperationResultDto? Operation = null
);

public sealed record NotificationReadAllResultDto(
    int Marked,
    OperationResultDto Operation);

public sealed record UnreadCountDto(int Count);

public sealed record LiveUpdateEnvelope(
    string Type,
    DateTimeOffset Timestamp,
    object Payload,
    long Sequence,
    string Channel
);

public sealed record LiveCursorDto(
    long Sequence,
    DateTimeOffset Timestamp,
    string Mode
);

public sealed record NotificationSnapshotDto(
    List<NotificationDto> Notifications,
    int UnreadCount,
    string ForUser
);

public sealed record DashboardLiveSnapshotDto(
    BoardResponse Board,
    NotificationSnapshotDto Notifications,
    LiveCursorDto Cursor
);

public sealed record DashboardLiveEventDto(
    LiveUpdateEnvelope Envelope,
    LiveCursorDto Cursor
);
