using Nexus.Api.Data;
using Nexus.Api.Models;

namespace Nexus.Api.Repositories;

public interface IOpenClawRunRepository
{
    Task<IReadOnlyList<OpenClawRun>> GetAsync(OpenClawRunQuery query, CancellationToken cancellationToken = default);
    Task<OpenClawRun?> GetByIdAsync(Guid id, bool tracking = false, CancellationToken cancellationToken = default);
    Task<OpenClawRun?> GetByStartIdempotencyKeyAsync(string idempotencyKey, CancellationToken cancellationToken = default);
    Task<OpenClawRun?> GetByOpenClawRunIdAsync(string openClawRunId, CancellationToken cancellationToken = default);
    Task<IReadOnlyList<OpenClawRunHistory>> GetHistoryAsync(Guid runId, CancellationToken cancellationToken = default);
    Task<OpenClawRunHistory?> GetInvocationAsync(
        Guid runId,
        string action,
        string idempotencyKey,
        CancellationToken cancellationToken = default);
    Task<bool> HasGatewayEventAsync(string gatewayEventId, CancellationToken cancellationToken = default);
    Task<bool> TaskExistsAsync(Guid taskId, CancellationToken cancellationToken = default);
    Task<bool> ProjectExistsAsync(Guid projectId, CancellationToken cancellationToken = default);
    Task<IReadOnlyList<OpenClawRunSubscription>> GetActiveSubscriptionsAsync(
        CancellationToken cancellationToken = default);
    Task AddAsync(
        OpenClawRun run,
        OpenClawRunHistory history,
        CancellationToken cancellationToken = default);
    Task AddRetryAsync(
        OpenClawRun source,
        OpenClawRun retry,
        OpenClawRunHistory sourceHistory,
        OpenClawRunHistory retryHistory,
        CancellationToken cancellationToken = default);
    Task UpdateAsync(
        OpenClawRun run,
        OpenClawRunHistory history,
        CancellationToken cancellationToken = default);
    Task UpdateProjectionAsync(
        OpenClawRun run,
        OpenClawRunHistory? history = null,
        CancellationToken cancellationToken = default);
}

public sealed record OpenClawRunSubscription(string SessionKey, string AgentId);
