using System.Text.Json;

namespace Tiku.Application.Growth;

public sealed record CommissionAdminActor(Guid TenantId, Guid UserId);

public sealed record UpdateCommissionSettingsCommand(decimal? DefaultRate, int? MinSettlementCents, string? SettlementCycle, JsonElement? Config);

public sealed record UpdateMemberCommissionRateCommand(Guid UserId, decimal? CommissionRate, JsonElement? CommissionConfig);

public sealed record CommissionPeriodQuery(DateOnly? StartDate = null, DateOnly? EndDate = null, Guid? ReferrerUserId = null, int? Limit = null);

public sealed record CommissionSettlementQuery(string? Status = null, Guid? ReferrerUserId = null, int? Limit = null);

public sealed record GenerateCommissionSettlementCommand(DateOnly StartDate, DateOnly EndDate, Guid ReferrerUserId, string? Status, string? Remark, JsonElement? Metadata);

public sealed record UpdateCommissionSettlementStatusCommand(Guid SettlementId, string? Status, string? ReviewNote, string? PaymentMethod, string? PaymentAccount, JsonElement? Metadata);

public sealed record CreateCommissionProofCommand(Guid SettlementId, string? ProofType, string? Title, string? Description, Guid? AssetId, string? ExternalUrl, int? AmountCents, string? PaymentMethod, string? PaymentAccount, DateTimeOffset? PaidAt, JsonElement? Metadata);

public sealed record UpdateCommissionProofStatusCommand(Guid ProofId, string? Status, string? ReviewNote, JsonElement? Metadata);

public sealed record CommissionSettingsItem(decimal DefaultRate, int MinSettlementCents, string SettlementCycle, JsonElement Config);

public sealed record CommissionSourceItem(
    string SourceType,
    Guid SourceId,
    string? SourceNo,
    Guid ReferrerUserId,
    Guid? StudentUserId,
    int GrossAmountCents,
    decimal CommissionRate,
    int CommissionAmountCents,
    string RateSource,
    Guid? SettlementId);

public sealed record CommissionSummaryItem(int SourceCount, int PaidUserCount, int GrossAmountCents, int CommissionAmountCents);

public sealed record CommissionSettlementItemDto(
    Guid Id,
    string SettlementNo,
    Guid ReferrerUserId,
    string Status,
    DateOnly PeriodStart,
    DateOnly PeriodEnd,
    int SourceCount,
    int PaidUserCount,
    int GrossAmountCents,
    int CommissionAmountCents,
    DateTimeOffset? ReviewedAt,
    DateTimeOffset? PaidAt);

public sealed record CommissionProofItem(
    Guid Id,
    Guid SettlementId,
    string ProofType,
    string Status,
    string? Title,
    Guid? AssetId,
    string? ExternalUrl,
    int? AmountCents,
    string? PaymentMethod,
    string? PaymentAccount,
    DateTimeOffset? PaidAt,
    DateTimeOffset? ReviewedAt);

public sealed record CommissionExportItem(Guid SettlementId, string Filename, string Format, string MimeType, int RowCount, string ContentBase64, string Sha256, int SizeBytes);

public sealed record CommissionList<T>(IReadOnlyCollection<T> Items);

public interface ICommissionService
{
    Task<CommissionSettingsItem> GetSettingsAsync(CommissionAdminActor actor, CancellationToken cancellationToken = default);
    Task<CommissionSettingsItem> UpdateSettingsAsync(CommissionAdminActor actor, UpdateCommissionSettingsCommand command, CancellationToken cancellationToken = default);
    Task<object> UpdateMemberRateAsync(CommissionAdminActor actor, UpdateMemberCommissionRateCommand command, CancellationToken cancellationToken = default);
    Task<CommissionSummaryItem> GetSummaryAsync(CommissionAdminActor actor, CommissionPeriodQuery query, CancellationToken cancellationToken = default);
    Task<CommissionList<CommissionSourceItem>> GetOrdersAsync(CommissionAdminActor actor, CommissionPeriodQuery query, CancellationToken cancellationToken = default);
    Task<CommissionList<CommissionSettlementItemDto>> GetSettlementsAsync(CommissionAdminActor actor, CommissionSettlementQuery query, CancellationToken cancellationToken = default);
    Task<CommissionExportItem> ExportSettlementAsync(CommissionAdminActor actor, Guid settlementId, string? format, CancellationToken cancellationToken = default);
    Task<CommissionSettlementItemDto> GenerateSettlementAsync(CommissionAdminActor actor, GenerateCommissionSettlementCommand command, CancellationToken cancellationToken = default);
    Task<CommissionSettlementItemDto> UpdateSettlementStatusAsync(CommissionAdminActor actor, UpdateCommissionSettlementStatusCommand command, CancellationToken cancellationToken = default);
    Task<CommissionList<CommissionProofItem>> GetProofsAsync(CommissionAdminActor actor, Guid settlementId, CancellationToken cancellationToken = default);
    Task<CommissionProofItem> CreateProofAsync(CommissionAdminActor actor, CreateCommissionProofCommand command, CancellationToken cancellationToken = default);
    Task<CommissionProofItem> UpdateProofStatusAsync(CommissionAdminActor actor, UpdateCommissionProofStatusCommand command, CancellationToken cancellationToken = default);
}

public sealed class CommissionException(string message, string code) : Exception(message)
{
    public string Code { get; } = code;
}
