using KArtSell.BuildingBlocks.Versioning;

namespace KArtSell.Modules.ModelOperations.Application;

public sealed record ApprovedModelContext(
    string ScopeKey,
    VersionSet VersionSet,
    string LifecycleState,
    DateTimeOffset EffectiveAt);

public sealed record DueModelOperation(
    Guid ScheduleId,
    string OperationCode,
    string ScopeKey,
    string Cadence,
    string AutomationMode,
    string Queue,
    string IdempotencyKey,
    int ScheduleVersion,
    DateTimeOffset ScheduledFor,
    string CatchUpPolicy,
    int MaxCatchUp);

public sealed record ModelOperationRequest(
    Guid RequestId,
    Guid ScheduleId,
    string OperationCode,
    string ScopeKey,
    string AutomationMode,
    string IdempotencyKey,
    ApprovedModelContext Context,
    string CorrelationId,
    DateTimeOffset RequestedAt);

public interface IApprovedModelContextReader
{
    Task<ApprovedModelContext?> ReadAsync(string scopeKey, DateTimeOffset asOf, CancellationToken cancellationToken);
}

public interface IModelScheduleRepository
{
    Task<IReadOnlyList<DueModelOperation>> AcquireDueAsync(
        DateTimeOffset now,
        string leaseOwner,
        TimeSpan leaseDuration,
        int limit,
        CancellationToken cancellationToken);

    Task MarkDispatchedAsync(
        Guid scheduleId,
        string leaseOwner,
        string backgroundJobId,
        DateTimeOffset dispatchedAt,
        DateTimeOffset nextDueAt,
        CancellationToken cancellationToken);

    Task ReleaseAsync(
        Guid scheduleId,
        string leaseOwner,
        string reasonCode,
        DateTimeOffset releasedAt,
        CancellationToken cancellationToken);
}

public interface IModelOperationRequestRepository
{
    Task<bool> AddAsync(ModelOperationRequest request, CancellationToken cancellationToken);
}

public interface IModelOperationRequestService
{
    Task<ModelOperationRequest?> RequestAsync(
        Guid scheduleId,
        string operationCode,
        string scopeKey,
        string automationMode,
        string idempotencyKey,
        string correlationId,
        CancellationToken cancellationToken);
}
