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

namespace TakeoutSaaS.Domain.Membership.Repositories;

/// <summary>
/// 会员消息触达仓储。
/// </summary>
public interface IMemberMessageReachRepository
{
    /// <summary>
    /// 分页查询消息。
    /// </summary>
    Task<(IReadOnlyList<MemberReachMessage> Items, int Total)> SearchMessagesAsync(
        long tenantId,
        MemberMessageStatus? status,
        MemberMessageChannel? channel,
        string? keyword,
        int page,
        int pageSize,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 按标识查询消息。
    /// </summary>
    Task<MemberReachMessage?> FindMessageByIdAsync(long tenantId, long messageId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 新增消息。
    /// </summary>
    Task AddMessageAsync(MemberReachMessage message, CancellationToken cancellationToken = default);

    /// <summary>
    /// 更新消息。
    /// </summary>
    Task UpdateMessageAsync(MemberReachMessage message, CancellationToken cancellationToken = default);

    /// <summary>
    /// 删除消息。
    /// </summary>
    Task DeleteMessageAsync(MemberReachMessage message, CancellationToken cancellationToken = default);

    /// <summary>
    /// 查询消息收件明细。
    /// </summary>
    Task<IReadOnlyList<MemberReachRecipient>> GetRecipientsAsync(
        long tenantId,
        long messageId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 删除消息收件明细。
    /// </summary>
    Task RemoveRecipientsAsync(
        long tenantId,
        long messageId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 批量新增消息收件明细。
    /// </summary>
    Task AddRecipientsAsync(
        IReadOnlyCollection<MemberReachRecipient> recipients,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 分页查询模板。
    /// </summary>
    Task<(IReadOnlyList<MemberMessageTemplate> Items, int Total)> SearchTemplatesAsync(
        long tenantId,
        MemberMessageTemplateCategory? category,
        string? keyword,
        int page,
        int pageSize,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 按标识查询模板。
    /// </summary>
    Task<MemberMessageTemplate?> FindTemplateByIdAsync(long tenantId, long templateId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 按名称查询模板（忽略大小写）。
    /// </summary>
    Task<MemberMessageTemplate?> FindTemplateByNameAsync(long tenantId, string name, CancellationToken cancellationToken = default);

    /// <summary>
    /// 新增模板。
    /// </summary>
    Task AddTemplateAsync(MemberMessageTemplate template, CancellationToken cancellationToken = default);

    /// <summary>
    /// 更新模板。
    /// </summary>
    Task UpdateTemplateAsync(MemberMessageTemplate template, CancellationToken cancellationToken = default);

    /// <summary>
    /// 删除模板。
    /// </summary>
    Task DeleteTemplateAsync(MemberMessageTemplate template, CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取月度发送统计。
    /// </summary>
    Task<MemberMessageMonthlyStatsSnapshot> GetMonthlyStatsAsync(
        long tenantId,
        DateTime monthStartUtc,
        DateTime monthEndUtc,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 保存变更。
    /// </summary>
    Task SaveChangesAsync(CancellationToken cancellationToken = default);
}

/// <summary>
/// 会员消息月度统计快照。
/// </summary>
public sealed record MemberMessageMonthlyStatsSnapshot(
    int SentMessageCount,
    int ReachMemberCount,
    int SentRecipientCount,
    int ReadRecipientCount,
    int ConvertedRecipientCount);
