using System.Text.Json.Nodes;

namespace Nexus.Api.Models;

public sealed record OpenClawConnectionDto(
    string State,
    bool Configured,
    bool CredentialConfigured,
    bool Connected,
    string Endpoint,
    string? GatewayVersion,
    string? RequiredVersion,
    bool VersionPinned,
    bool VersionMatches,
    int? ProtocolVersion,
    IReadOnlyList<string> GrantedScopes,
    IReadOnlyList<string> AdvertisedEvents,
    DateTimeOffset? LastConnectedAt,
    DateTimeOffset? LastEventAt,
    int ReconnectAttempts,
    string? Message,
    string? Recovery,
    DateTimeOffset CheckedAt,
    string? DeviceId = null,
    bool PairingRequired = false,
    string? PairingRequestId = null);

public sealed record OpenClawCapabilityDto(
    string Id,
    string Label,
    string Method,
    string RequiredScope,
    bool Available,
    string State,
    string? Reason);

public sealed record OpenClawCollectionDto<T>(
    string State,
    IReadOnlyList<T> Items,
    string? NextCursor,
    string? Message,
    string? Recovery,
    DateTimeOffset CheckedAt);

public sealed record OpenClawOperationDto<T>(
    bool Ok,
    string State,
    string Message,
    T? Data,
    string? Recovery,
    DateTimeOffset CompletedAt,
    string? OperationId = null,
    string? CorrelationId = null,
    string? IdempotencyKey = null,
    string? TraceParent = null,
    string? Actor = null,
    OperationResultDto? Operation = null);

public sealed record OpenClawTaskDto(
    string Id,
    string Title,
    string Status,
    string? Kind,
    string? Runtime,
    string? AgentId,
    string? SessionKey,
    string? RunId,
    string? FlowId,
    string? ParentTaskId,
    DateTimeOffset? CreatedAt,
    DateTimeOffset? StartedAt,
    DateTimeOffset? UpdatedAt,
    DateTimeOffset? FinishedAt,
    double? Progress,
    string? Summary,
    string? Error,
    bool CanCancel);

public sealed record OpenClawSessionDto(
    string Key,
    string? SessionId,
    string AgentId,
    string Title,
    string Status,
    string? Kind,
    string? Channel,
    string? Model,
    string? Provider,
    string? RunId,
    DateTimeOffset? UpdatedAt,
    long? InputTokens,
    long? OutputTokens,
    long? TotalTokens,
    bool CanAbort);

public sealed record OpenClawCronJobDto(
    string Id,
    string Name,
    string? Description,
    string Schedule,
    string? TimeZone,
    bool Enabled,
    string Status,
    string? AgentId,
    string? SessionKey,
    DateTimeOffset? NextRunAt,
    DateTimeOffset? LastRunAt,
    string? LastRunStatus,
    string? LastError,
    bool CanRun,
    string? ResourceHash = null);

public sealed record OpenClawCronScheduleDto(
    string Kind,
    string? Expression,
    string? TimeZone,
    string? At,
    long? EveryMs,
    long? AnchorMs,
    long? StaggerMs,
    string? Command,
    string? WorkingDirectory);

public sealed record OpenClawCronPayloadDto(
    string Kind,
    string? Text,
    string? Message,
    string? Model,
    IReadOnlyList<string> Fallbacks,
    string? Thinking,
    double? TimeoutSeconds,
    bool? AllowUnsafeExternalContent,
    bool? LightContext,
    IReadOnlyList<string> ToolsAllow,
    IReadOnlyList<string> Arguments,
    string? WorkingDirectory,
    IReadOnlyList<string> EnvironmentKeys,
    bool InputConfigured,
    double? NoOutputTimeoutSeconds,
    int? OutputMaxBytes);

public sealed record OpenClawCronDestinationDto(
    string? Channel,
    string? Target,
    string? AccountId,
    string? Mode);

public sealed record OpenClawCronDeliveryDto(
    string Mode,
    string? Channel,
    string? Target,
    string? ThreadId,
    string? AccountId,
    bool? BestEffort,
    OpenClawCronDestinationDto? CompletionDestination,
    OpenClawCronDestinationDto? FailureDestination);

public sealed record OpenClawCronTriggerDto(
    string Script,
    bool Once);

public sealed record OpenClawCronFailureAlertDto(
    int? After,
    string? Channel,
    string? Target,
    long? CooldownMs,
    bool? IncludeSkipped,
    string? Mode,
    string? AccountId);

public sealed record OpenClawCronJobDetailDto(
    string Id,
    string Name,
    string? DisplayName,
    string? Description,
    bool Enabled,
    bool DeleteAfterRun,
    string? AgentId,
    string? SessionKey,
    string SessionTarget,
    string WakeMode,
    OpenClawCronScheduleDto Schedule,
    OpenClawCronPayloadDto Payload,
    OpenClawCronDeliveryDto? Delivery,
    OpenClawCronTriggerDto? Trigger,
    OpenClawCronFailureAlertDto? FailureAlert,
    DateTimeOffset? CreatedAt,
    DateTimeOffset? UpdatedAt,
    DateTimeOffset? NextRunAt,
    DateTimeOffset? LastRunAt,
    string? LastRunStatus,
    string? LastError,
    string ResourceHash,
    bool CanUpdate,
    bool CanDelete,
    bool CanRun);

public sealed record OpenClawCronRunDto(
    string Id,
    string JobId,
    string? JobName,
    string? RunId,
    string Status,
    string Action,
    string? Summary,
    string? Error,
    string? ErrorReason,
    string? DeliveryStatus,
    string? DeliveryError,
    bool? Delivered,
    bool? TriggerFired,
    string? DiagnosticsSummary,
    IReadOnlyList<OpenClawCronRunDiagnosticDto> Diagnostics,
    string? SessionId,
    string? SessionKey,
    DateTimeOffset? OccurredAt,
    DateTimeOffset? RunAt,
    long? DurationMs,
    DateTimeOffset? NextRunAt,
    string? Model,
    string? Provider,
    long? InputTokens,
    long? OutputTokens,
    long? TotalTokens);

public sealed record OpenClawCronRunDiagnosticDto(
    DateTimeOffset? OccurredAt,
    string Source,
    string Severity,
    string Message,
    string? ToolName,
    double? ExitCode,
    bool Truncated);

public sealed record CreateOpenClawCronJobRequest(
    string Name,
    JsonObject Schedule,
    string SessionTarget,
    string WakeMode,
    JsonObject Payload,
    string? Description = null,
    bool Enabled = true,
    string? AgentId = null,
    string? SessionKey = null,
    bool? DeleteAfterRun = null,
    JsonObject? Delivery = null,
    JsonObject? Trigger = null,
    JsonNode? FailureAlert = null,
    string? DeclarationKey = null,
    string? DisplayName = null);

public sealed record PatchOpenClawCronJobRequest(
    JsonObject Patch,
    string? ExpectedHash = null);

public sealed record OpenClawActivityDto(
    string Id,
    string EventType,
    string Kind,
    string Action,
    string Status,
    string Message,
    string? Severity,
    string? Actor,
    string? AgentId,
    string? SessionKey,
    string? RunId,
    DateTimeOffset? OccurredAt,
    string Source);

public sealed record OpenClawApprovalDto(
    string Id,
    string Kind,
    string Title,
    string? Description,
    string Status,
    string Severity,
    string? Command,
    string? WorkingDirectory,
    string? AgentId,
    string? SessionKey,
    DateTimeOffset? RequestedAt,
    DateTimeOffset? ExpiresAt,
    IReadOnlyList<string> AllowedDecisions,
    bool CanResolve);

public sealed record OpenClawModelDto(
    string Id,
    string Name,
    string Provider,
    bool Configured,
    bool Available,
    int? ContextWindow,
    string? Reason);

public sealed record OpenClawModelAuthExpiryDto(
    DateTimeOffset At,
    long RemainingMs,
    string Label);

public sealed record OpenClawModelAuthProfileSummaryDto(
    string Type,
    string Status,
    int Count);

public sealed record OpenClawModelAuthApiKeyDto(
    string Source,
    string? EnvVar);

public sealed record OpenClawModelAuthUsageDto(
    string? Summary,
    string? Plan);

/// <summary>
/// Browser-safe projection of OpenClaw models.authStatus.
/// Profile identifiers, account identities, billing details and credentials are
/// deliberately absent from this public contract.
/// </summary>
public sealed record OpenClawModelAuthProviderDto(
    string Provider,
    string DisplayName,
    string Status,
    OpenClawModelAuthExpiryDto? Expiry,
    IReadOnlyList<OpenClawModelAuthProfileSummaryDto> Profiles,
    OpenClawModelAuthApiKeyDto? ApiKey,
    OpenClawModelAuthUsageDto? Usage);

public sealed record OpenClawAgentDto(
    string Id,
    string Name,
    string? Description,
    string? Model,
    string? Provider,
    string? Workspace,
    string Status);

public sealed record OpenClawOverviewDto(
    OpenClawConnectionDto Connection,
    IReadOnlyList<OpenClawCapabilityDto> Capabilities,
    OpenClawCollectionDto<OpenClawTaskDto> Tasks,
    OpenClawCollectionDto<OpenClawSessionDto> Sessions,
    OpenClawCollectionDto<OpenClawCronJobDto> CronJobs,
    OpenClawCollectionDto<OpenClawApprovalDto> Approvals,
    OpenClawCollectionDto<OpenClawActivityDto> Activity,
    OpenClawCollectionDto<OpenClawModelDto> Models,
    OpenClawCollectionDto<OpenClawAgentDto> Agents,
    DateTimeOffset GeneratedAt);

public sealed record CancelOpenClawTaskRequest(string? Reason);

public sealed record AbortOpenClawSessionRequest(
    string SessionKey,
    string? RunId = null,
    bool ClearQueued = true);

public sealed record ResolveOpenClawApprovalRequest(
    string Kind,
    string Decision);

public sealed record PatchOpenClawSessionModelRequest(
    string SessionKey,
    string Model);
