using System.Text.Json.Nodes;

namespace Nexus.Api.Models;

public sealed record StartOpenClawRunRequest(
    string Prompt,
    string AgentId,
    string SessionKey,
    string? Title = null,
    Guid? TaskId = null,
    Guid? ProjectId = null);

public sealed record OpenClawRunActionRequest(string? Reason = null);

/// <summary>
/// Trusted invocation context assembled by the Nexus HTTP boundary. Actor is
/// derived from the authenticated principal and is never accepted from JSON.
/// </summary>
public sealed record OpenClawInvocationMetadata(
    string IdempotencyKey,
    string CorrelationId,
    string Actor,
    string? TraceParent);

public sealed record OpenClawRunDto(
    Guid Id,
    string Title,
    string Prompt,
    string AgentId,
    string SessionKey,
    string Status,
    Guid? TaskId,
    Guid? ProjectId,
    string? OpenClawRunId,
    Guid? RetriedFromRunId,
    string CorrelationId,
    string Actor,
    string? LastError,
    long? LastGatewaySequence,
    bool SequenceGapDetected,
    bool CanStop,
    bool CanRetry,
    bool CanResume,
    string? ResumeCapabilityMessage,
    DateTimeOffset CreatedAt,
    DateTimeOffset UpdatedAt,
    DateTimeOffset? StartedAt,
    DateTimeOffset? FinishedAt);

public sealed record OpenClawRunHistoryDto(
    long Id,
    Guid RunId,
    string Action,
    string FromStatus,
    string ToStatus,
    string Message,
    string Actor,
    string CorrelationId,
    string? IdempotencyKey,
    string? TraceParent,
    string? GatewayEventId,
    long? GatewaySequence,
    bool SequenceGapDetected,
    Guid? ResultRunId,
    DateTimeOffset OccurredAt);

public sealed record OpenClawRunCollectionDto(
    IReadOnlyList<OpenClawRunDto> Items,
    string? NextCursor,
    DateTimeOffset CheckedAt);

public sealed record OpenClawRunOperationDto(
    bool Ok,
    string State,
    string Message,
    OpenClawRunDto Run,
    OpenClawRunDto? ResultRun,
    DateTimeOffset CompletedAt,
    OperationResultDto? Operation = null);

public sealed record OpenClawRunHistoryResponse(
    OpenClawRunDto Run,
    IReadOnlyList<OpenClawRunHistoryDto> Transitions,
    string GatewayHistoryState,
    JsonNode? GatewayHistory,
    string? Message,
    DateTimeOffset CheckedAt);

public sealed record OpenClawRunQuery(
    int Limit = 50,
    string? Cursor = null,
    string? Status = null,
    Guid? TaskId = null,
    Guid? ProjectId = null,
    string? SessionKey = null);
