using TakeoutSaaS.Domain.Membership.Entities;
using TakeoutSaaS.Domain.Membership.Enums;

namespace TakeoutSaaS.Domain.Membership.Repositories;

/// <summary>
/// 储值卡模块仓储契约。
/// </summary>
public interface IStoredCardRepository
{
    /// <summary>
    /// 查询门店下全部储值方案。
    /// </summary>
    Task<IReadOnlyList<MemberStoredCardPlan>> GetPlansByStoreAsync(
        long tenantId,
        long storeId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 按标识查询储值方案。
    /// </summary>
    Task<MemberStoredCardPlan?> FindPlanByIdAsync(
        long tenantId,
        long storeId,
        long planId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 新增储值方案。
    /// </summary>
    Task AddPlanAsync(MemberStoredCardPlan entity, CancellationToken cancellationToken = default);

    /// <summary>
    /// 更新储值方案。
    /// </summary>
    Task UpdatePlanAsync(MemberStoredCardPlan entity, CancellationToken cancellationToken = default);

    /// <summary>
    /// 删除储值方案。
    /// </summary>
    Task DeletePlanAsync(MemberStoredCardPlan entity, CancellationToken cancellationToken = default);

    /// <summary>
    /// 按方案聚合充值次数与金额。
    /// </summary>
    Task<Dictionary<long, MemberStoredCardPlanAggregateSnapshot>> GetPlanAggregatesAsync(
        long tenantId,
        long storeId,
        IReadOnlyCollection<long> planIds,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取储值方案页统计快照。
    /// </summary>
    Task<MemberStoredCardPlanStatsSnapshot> GetPlanStatsAsync(
        long tenantId,
        long storeId,
        DateTime nowUtc,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 查询充值记录分页。
    /// </summary>
    Task<(IReadOnlyList<MemberStoredCardRechargeRecord> Items, int TotalCount)> SearchRechargeRecordsAsync(
        long tenantId,
        long storeId,
        DateTime? startUtc,
        DateTime? endUtc,
        string? keyword,
        int page,
        int pageSize,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 查询充值记录导出数据。
    /// </summary>
    Task<IReadOnlyList<MemberStoredCardRechargeRecord>> ListRechargeRecordsForExportAsync(
        long tenantId,
        long storeId,
        DateTime? startUtc,
        DateTime? endUtc,
        string? keyword,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 新增充值记录。
    /// </summary>
    Task AddRechargeRecordAsync(MemberStoredCardRechargeRecord entity, CancellationToken cancellationToken = default);

    /// <summary>
    /// 持久化变更。
    /// </summary>
    Task SaveChangesAsync(CancellationToken cancellationToken = default);
}

/// <summary>
/// 储值方案聚合快照。
/// </summary>
public sealed record MemberStoredCardPlanAggregateSnapshot
{
    /// <summary>
    /// 方案标识。
    /// </summary>
    public required long PlanId { get; init; }

    /// <summary>
    /// 充值次数。
    /// </summary>
    public int RechargeCount { get; init; }

    /// <summary>
    /// 充值总额。
    /// </summary>
    public decimal TotalRechargeAmount { get; init; }
}

/// <summary>
/// 储值卡统计快照。
/// </summary>
public sealed record MemberStoredCardPlanStatsSnapshot
{
    /// <summary>
    /// 储值总额。
    /// </summary>
    public decimal TotalRechargeAmount { get; init; }

    /// <summary>
    /// 赠金总额。
    /// </summary>
    public decimal TotalGiftAmount { get; init; }

    /// <summary>
    /// 本月充值额。
    /// </summary>
    public decimal CurrentMonthRechargeAmount { get; init; }

    /// <summary>
    /// 储值用户数。
    /// </summary>
    public int RechargeMemberCount { get; init; }
}
