namespace YMhut.Box.Core.Feedback;

public sealed record FeedbackRequest(
    string Title,
    string Type,
    string Severity,
    string Contact,
    string Body,
    bool IncludeTodayLogs,
    bool IncludeToolStatus,
    bool IncludeSystemSummary,
    string? ScreenshotPath = null,
    string Language = "zh-CN",
    string? FeedbackCode = null);

public sealed record FeedbackPackageResult(
    string PackagePath,
    string PackageSha256,
    long PackageBytes,
    DateTimeOffset CreatedAt,
    string SummaryText,
    IReadOnlyDictionary<string, string> IncludedFiles);

public sealed record FeedbackSubmissionResponse(
    bool Ok,
    string? Code,
    string? ErrorCode,
    string? Message,
    bool Duplicate = false);

public sealed record FeedbackStatusResponse(
    bool Ok,
    string? Code,
    string? Status,
    string? StatusLabel,
    bool HasReply,
    string? Reply,
    string? ReceivedAt,
    string? UpdatedAt,
    string? ErrorCode,
    string? Message,
    string? StatusDetail = null,
    string? Category = null,
    string? Priority = null,
    bool MailSent = false);

public enum FeedbackRecordState
{
    Draft,
    PackageCreated,
    PackageFailed,
    Sending,
    Sent,
    SendFailed,
    Querying,
    StatusUpdated,
    QueryFailed,
    Archived
}

public sealed record FeedbackRecord(
    string Code,
    string Title,
    string Type,
    string Severity,
    DateTimeOffset CreatedAtUtc,
    DateTimeOffset UpdatedAtUtc,
    FeedbackRecordState State,
    string StatusLabel,
    string LastMessage,
    string? PackagePath = null,
    string? PackageSha256 = null,
    long PackageBytes = 0,
    DateTimeOffset? SubmittedAtUtc = null,
    DateTimeOffset? LastQueryAtUtc = null,
    DateTimeOffset? ArchivedAtUtc = null,
    bool IsArchived = false,
    int FailureCount = 0,
    string? ServiceStatus = null,
    string? PublicReply = null,
    string? ServiceStatusDetail = null,
    string? ServiceCategory = null,
    string? ServicePriority = null,
    bool? ServiceMailSent = null,
    string? ServiceReceivedAt = null,
    string? ServiceUpdatedAt = null);

public interface IFeedbackPackageService
{
    Task<FeedbackPackageResult> BuildPackageAsync(
        FeedbackRequest request,
        CancellationToken cancellationToken = default);
}

public interface IFeedbackSubmissionService
{
    Task<FeedbackSubmissionResponse> SubmitAsync(
        FeedbackRequest request,
        FeedbackPackageResult package,
        CancellationToken cancellationToken = default);

    Task<FeedbackStatusResponse> GetStatusAsync(
        string feedbackCode,
        CancellationToken cancellationToken = default);
}

public interface IFeedbackRecordStore
{
    Task<IReadOnlyList<FeedbackRecord>> ReadAsync(CancellationToken cancellationToken = default);

    Task<FeedbackRecord?> FindAsync(string feedbackCode, CancellationToken cancellationToken = default);

    Task<FeedbackRecord> UpsertAsync(FeedbackRecord record, CancellationToken cancellationToken = default);

    Task RemoveAsync(string feedbackCode, CancellationToken cancellationToken = default);

    Task<IReadOnlyList<FeedbackRecord>> ArchiveOlderThanAsync(
        TimeSpan activeWindow,
        DateTimeOffset? now = null,
        CancellationToken cancellationToken = default);
}
