using TakeoutSaaS.Application.App.Merchants.Dto;
using TakeoutSaaS.Domain.Merchants.Entities;

namespace TakeoutSaaS.Application.App.Merchants;

/// <summary>
/// 商户 DTO 映射工具。
/// </summary>
internal static class MerchantMapping
{
    /// <summary>
    /// 将商户实体映射为 DTO。
    /// </summary>
    /// <param name="merchant">商户实体。</param>
    /// <returns>商户 DTO。</returns>
    public static MerchantDto ToDto(Merchant merchant) => new()
    {
        Id = merchant.Id,
        TenantId = merchant.TenantId,
        BrandName = merchant.BrandName,
        BrandAlias = merchant.BrandAlias,
        LogoUrl = merchant.LogoUrl,
        Category = merchant.Category,
        ContactPhone = merchant.ContactPhone,
        ContactEmail = merchant.ContactEmail,
        Status = merchant.Status,
        JoinedAt = merchant.JoinedAt,
        CreatedAt = merchant.CreatedAt
    };

    /// <summary>
    /// 将商户证照实体映射为 DTO。
    /// </summary>
    /// <param name="document">证照实体。</param>
    /// <returns>证照 DTO。</returns>
    public static MerchantDocumentDto ToDto(MerchantDocument document) => new()
    {
        Id = document.Id,
        MerchantId = document.MerchantId,
        DocumentType = document.DocumentType,
        Status = document.Status,
        FileUrl = document.FileUrl,
        DocumentNumber = document.DocumentNumber,
        IssuedAt = document.IssuedAt,
        ExpiresAt = document.ExpiresAt,
        Remarks = document.Remarks,
        CreatedAt = document.CreatedAt
    };

    /// <summary>
    /// 将商户合同实体映射为 DTO。
    /// </summary>
    /// <param name="contract">合同实体。</param>
    /// <returns>合同 DTO。</returns>
    public static MerchantContractDto ToDto(MerchantContract contract) => new()
    {
        Id = contract.Id,
        MerchantId = contract.MerchantId,
        ContractNumber = contract.ContractNumber,
        Status = contract.Status,
        StartDate = contract.StartDate,
        EndDate = contract.EndDate,
        FileUrl = contract.FileUrl,
        SignedAt = contract.SignedAt,
        TerminatedAt = contract.TerminatedAt,
        TerminationReason = contract.TerminationReason
    };

    /// <summary>
    /// 将商户审核日志实体映射为 DTO。
    /// </summary>
    /// <param name="log">审核日志实体。</param>
    /// <returns>审核日志 DTO。</returns>
    public static MerchantAuditLogDto ToDto(MerchantAuditLog log) => new()
    {
        Id = log.Id,
        MerchantId = log.MerchantId,
        Action = log.Action,
        Title = log.Title,
        Description = log.Description,
        OperatorName = log.OperatorName,
        CreatedAt = log.CreatedAt
    };

    /// <summary>
    /// 将商户分类实体映射为 DTO。
    /// </summary>
    /// <param name="category">分类实体。</param>
    /// <returns>分类 DTO。</returns>
    public static MerchantCategoryDto ToDto(MerchantCategory category) => new()
    {
        Id = category.Id,
        Name = category.Name,
        DisplayOrder = category.DisplayOrder,
        IsActive = category.IsActive,
        CreatedAt = category.CreatedAt
    };

    /// <summary>
    /// 将证照集合映射为 DTO 集合。
    /// </summary>
    /// <param name="documents">证照集合。</param>
    /// <returns>证照 DTO 列表。</returns>
    public static IReadOnlyList<MerchantDocumentDto> ToDocumentDtos(IEnumerable<MerchantDocument> documents)
        => documents.Select(ToDto).ToList();

    /// <summary>
    /// 将合同集合映射为 DTO 集合。
    /// </summary>
    /// <param name="contracts">合同集合。</param>
    /// <returns>合同 DTO 列表。</returns>
    public static IReadOnlyList<MerchantContractDto> ToContractDtos(IEnumerable<MerchantContract> contracts)
        => contracts.Select(ToDto).ToList();

    /// <summary>
    /// 将分类集合映射为 DTO 集合。
    /// </summary>
    /// <param name="categories">分类集合。</param>
    /// <returns>分类 DTO 列表。</returns>
    public static IReadOnlyList<MerchantCategoryDto> ToCategoryDtos(IEnumerable<MerchantCategory> categories)
        => categories.Select(ToDto).ToList();
}
