using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text.Json;
using Tiku.Application.Growth;

namespace Tiku.Api.Contracts;

public sealed class UpdateCommissionSettingsDto
{
    [Range(0, 1)]
    public decimal? DefaultRate { get; set; }
    [Range(0, int.MaxValue)]
    public int? MinSettlementCents { get; set; }
    [StringLength(50)]
    public string? SettlementCycle { get; set; }
    public JsonElement? Config { get; set; }
    public UpdateCommissionSettingsCommand ToCommand() => new(DefaultRate, MinSettlementCents, SettlementCycle, Config);
}

public sealed class UpdateMemberCommissionRateDto
{
    [Required]
    public Guid UserId { get; set; }
    [Range(0, 1)]
    public decimal? CommissionRate { get; set; }
    public JsonElement? CommissionConfig { get; set; }
    public UpdateMemberCommissionRateCommand ToCommand() => new(UserId, CommissionRate, CommissionConfig);
}

public class CommissionPeriodQueryDto
{
    [RegularExpression("^\\d{4}-\\d{2}-\\d{2}$")]
    public string? StartDate { get; set; }
    [RegularExpression("^\\d{4}-\\d{2}-\\d{2}$")]
    public string? EndDate { get; set; }
    public Guid? ReferrerUserId { get; set; }
    [Range(1, 500)]
    public int? Limit { get; set; }
    public CommissionPeriodQuery ToQuery() => new(ParseDate(StartDate), ParseDate(EndDate), ReferrerUserId, Limit);
    protected static DateOnly? ParseDate(string? value) => string.IsNullOrWhiteSpace(value) ? null : DateOnly.ParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture);
}

public sealed class CommissionSettlementsQueryDto
{
    [StringLength(50)]
    public string? Status { get; set; }
    public Guid? ReferrerUserId { get; set; }
    [Range(1, 500)]
    public int? Limit { get; set; }
    public CommissionSettlementQuery ToQuery() => new(Status, ReferrerUserId, Limit);
}

public sealed class GenerateCommissionSettlementDto
{
    [Required]
    [RegularExpression("^\\d{4}-\\d{2}-\\d{2}$")]
    public string StartDate { get; set; } = string.Empty;
    [Required]
    [RegularExpression("^\\d{4}-\\d{2}-\\d{2}$")]
    public string EndDate { get; set; } = string.Empty;
    [Required]
    public Guid ReferrerUserId { get; set; }
    [StringLength(50)]
    public string? Status { get; set; }
    [StringLength(1000)]
    public string? Remark { get; set; }
    public JsonElement? Metadata { get; set; }
    public GenerateCommissionSettlementCommand ToCommand() => new(Parse(StartDate), Parse(EndDate), ReferrerUserId, Status, Remark, Metadata);
    private static DateOnly Parse(string value) => DateOnly.ParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture);
}

public sealed class UpdateCommissionSettlementStatusDto
{
    [Required]
    public Guid SettlementId { get; set; }
    [StringLength(50)]
    public string? Status { get; set; }
    [StringLength(1000)]
    public string? ReviewNote { get; set; }
    [StringLength(100)]
    public string? PaymentMethod { get; set; }
    [StringLength(200)]
    public string? PaymentAccount { get; set; }
    public JsonElement? Metadata { get; set; }
    public UpdateCommissionSettlementStatusCommand ToCommand() => new(SettlementId, Status, ReviewNote, PaymentMethod, PaymentAccount, Metadata);
}

public sealed class CommissionSettlementExportQueryDto
{
    [Required]
    public Guid SettlementId { get; set; }
    [StringLength(10)]
    public string? Format { get; set; }
}

public sealed class CommissionSettlementProofQueryDto
{
    [Required]
    public Guid SettlementId { get; set; }
}

public sealed class CreateCommissionProofDto
{
    [Required]
    public Guid SettlementId { get; set; }
    [StringLength(50)]
    public string? ProofType { get; set; }
    [StringLength(200)]
    public string? Title { get; set; }
    [StringLength(2000)]
    public string? Description { get; set; }
    public Guid? AssetId { get; set; }
    [StringLength(2048)]
    public string? ExternalUrl { get; set; }
    [Range(0, int.MaxValue)]
    public int? AmountCents { get; set; }
    [StringLength(100)]
    public string? PaymentMethod { get; set; }
    [StringLength(200)]
    public string? PaymentAccount { get; set; }
    public DateTimeOffset? PaidAt { get; set; }
    public JsonElement? Metadata { get; set; }
    public CreateCommissionProofCommand ToCommand() => new(SettlementId, ProofType, Title, Description, AssetId, ExternalUrl, AmountCents, PaymentMethod, PaymentAccount, PaidAt, Metadata);
}

public sealed class UpdateCommissionProofStatusDto
{
    [Required]
    public Guid ProofId { get; set; }
    [StringLength(50)]
    public string? Status { get; set; }
    [StringLength(1000)]
    public string? ReviewNote { get; set; }
    public JsonElement? Metadata { get; set; }
    public UpdateCommissionProofStatusCommand ToCommand() => new(ProofId, Status, ReviewNote, Metadata);
}
