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

namespace TakeoutSaaS.Application.App.Stores;

/// <summary>
/// 门店相关映射助手。
/// </summary>
public static class StoreMapping
{
    /// <summary>
    /// 映射门店 DTO。
    /// </summary>
    /// <param name="store">门店实体。</param>
    /// <returns>DTO。</returns>
    public static StoreDto ToDto(Store store) => new()
    {
        Id = store.Id,
        TenantId = store.TenantId,
        MerchantId = store.MerchantId,
        Code = store.Code,
        Name = store.Name,
        Phone = store.Phone,
        ManagerName = store.ManagerName,
        Status = store.Status,
        SignboardImageUrl = store.SignboardImageUrl,
        OwnershipType = store.OwnershipType,
        AuditStatus = store.AuditStatus,
        BusinessStatus = store.BusinessStatus,
        ClosureReason = store.ClosureReason,
        ClosureReasonText = store.ClosureReasonText,
        CategoryId = store.CategoryId,
        RejectionReason = store.RejectionReason,
        SubmittedAt = store.SubmittedAt,
        ActivatedAt = store.ActivatedAt,
        ForceClosedAt = store.ForceClosedAt,
        ForceCloseReason = store.ForceCloseReason,
        Province = store.Province,
        City = store.City,
        District = store.District,
        Address = store.Address,
        Longitude = store.Longitude,
        Latitude = store.Latitude,
        GeoStatus = store.GeoStatus,
        GeoFailReason = store.GeoFailReason,
        GeoUpdatedAt = store.GeoUpdatedAt,
        Announcement = store.Announcement,
        Tags = store.Tags,
        DeliveryRadiusKm = store.DeliveryRadiusKm,
        SupportsDineIn = store.SupportsDineIn,
        SupportsPickup = store.SupportsPickup,
        SupportsDelivery = store.SupportsDelivery,
        SupportsReservation = store.SupportsReservation,
        SupportsQueueing = store.SupportsQueueing,
        CreatedAt = store.CreatedAt
    };

    /// <summary>
    /// 映射门店费用 DTO。
    /// </summary>
    /// <param name="fee">费用配置。</param>
    /// <returns>DTO。</returns>
    public static StoreFeeDto ToDto(StoreFee fee) => new()
    {
        Id = fee.Id,
        StoreId = fee.StoreId,
        MinimumOrderAmount = fee.MinimumOrderAmount,
        DeliveryFee = fee.BaseDeliveryFee,
        PlatformServiceRate = fee.PlatformServiceRate,
        CutleryFeeEnabled = fee.CutleryFeeEnabled,
        CutleryFeeAmount = fee.CutleryFeeAmount,
        RushFeeEnabled = fee.RushFeeEnabled,
        RushFeeAmount = fee.RushFeeAmount,
        PackagingFeeMode = fee.PackagingFeeMode,
        OrderPackagingFeeMode = fee.OrderPackagingFeeMode,
        FixedPackagingFee = fee.FixedPackagingFee,
        PackagingFeeTiers = StoreFeeTierHelper.Deserialize(fee.PackagingFeeTiersJson),
        FreeDeliveryThreshold = fee.FreeDeliveryThreshold,
        CreatedAt = fee.CreatedAt,
        UpdatedAt = fee.UpdatedAt
    };

    /// <summary>
    /// 映射门店资质 DTO。
    /// </summary>
    /// <param name="qualification">资质实体。</param>
    /// <returns>DTO。</returns>
    public static StoreQualificationDto ToDto(StoreQualification qualification)
    {
        var today = DateOnly.FromDateTime(DateTime.UtcNow);
        int? daysUntilExpiry = qualification.ExpiresAt.HasValue
            ? qualification.ExpiresAt.Value.DayNumber - today.DayNumber
            : null;

        return new StoreQualificationDto
        {
            Id = qualification.Id,
            StoreId = qualification.StoreId,
            QualificationType = qualification.QualificationType,
            FileUrl = qualification.FileUrl,
            DocumentNumber = qualification.DocumentNumber,
            IssuedAt = qualification.IssuedAt,
            ExpiresAt = qualification.ExpiresAt,
            IsExpired = qualification.IsExpired,
            IsExpiringSoon = qualification.IsExpiringSoon,
            DaysUntilExpiry = daysUntilExpiry,
            SortOrder = qualification.SortOrder,
            CreatedAt = qualification.CreatedAt,
            UpdatedAt = qualification.UpdatedAt
        };
    }

    /// <summary>
    /// 映射营业时段 DTO。
    /// </summary>
    /// <param name="hour">营业时段实体。</param>
    /// <returns>DTO。</returns>
    public static StoreBusinessHourDto ToDto(StoreBusinessHour hour) => new()
    {
        Id = hour.Id,
        TenantId = hour.TenantId,
        StoreId = hour.StoreId,
        DayOfWeek = hour.DayOfWeek,
        HourType = hour.HourType,
        StartTime = hour.StartTime,
        EndTime = hour.EndTime,
        CapacityLimit = hour.CapacityLimit,
        Notes = hour.Notes,
        CreatedAt = hour.CreatedAt
    };

    /// <summary>
    /// 映射配送区域 DTO。
    /// </summary>
    /// <param name="zone">配送区域实体。</param>
    /// <returns>DTO。</returns>
    public static StoreDeliveryZoneDto ToDto(StoreDeliveryZone zone) => new()
    {
        Id = zone.Id,
        TenantId = zone.TenantId,
        StoreId = zone.StoreId,
        ZoneName = zone.ZoneName,
        PolygonGeoJson = zone.PolygonGeoJson,
        MinimumOrderAmount = zone.MinimumOrderAmount,
        DeliveryFee = zone.DeliveryFee,
        EstimatedMinutes = zone.EstimatedMinutes,
        Color = zone.Color,
        Priority = zone.Priority,
        SortOrder = zone.SortOrder,
        CreatedAt = zone.CreatedAt
    };

    /// <summary>
    /// 映射节假日 DTO。
    /// </summary>
    /// <param name="holiday">节假日实体。</param>
    /// <returns>DTO。</returns>
    public static StoreHolidayDto ToDto(StoreHoliday holiday) => new()
    {
        Id = holiday.Id,
        TenantId = holiday.TenantId,
        StoreId = holiday.StoreId,
        Date = holiday.Date,
        EndDate = holiday.EndDate,
        IsAllDay = holiday.IsAllDay,
        StartTime = holiday.StartTime,
        EndTime = holiday.EndTime,
        OverrideType = holiday.OverrideType,
        IsClosed = holiday.IsClosed,
        Reason = holiday.Reason,
        CreatedAt = holiday.CreatedAt
    };

    /// <summary>
    /// 映射桌台区域 DTO。
    /// </summary>
    /// <param name="area">区域实体。</param>
    /// <returns>DTO。</returns>
    public static StoreTableAreaDto ToDto(StoreTableArea area) => new()
    {
        Id = area.Id,
        TenantId = area.TenantId,
        StoreId = area.StoreId,
        Name = area.Name,
        Description = area.Description,
        SortOrder = area.SortOrder,
        CreatedAt = area.CreatedAt
    };

    /// <summary>
    /// 映射桌台 DTO。
    /// </summary>
    /// <param name="table">桌台实体。</param>
    /// <returns>DTO。</returns>
    public static StoreTableDto ToDto(StoreTable table) => new()
    {
        Id = table.Id,
        TenantId = table.TenantId,
        StoreId = table.StoreId,
        AreaId = table.AreaId,
        TableCode = table.TableCode,
        Capacity = table.Capacity,
        Tags = table.Tags,
        Status = table.Status,
        QrCodeUrl = table.QrCodeUrl,
        CreatedAt = table.CreatedAt
    };

    /// <summary>
    /// 映射排班 DTO。
    /// </summary>
    /// <param name="shift">排班实体。</param>
    /// <returns>DTO。</returns>
    public static StoreEmployeeShiftDto ToDto(StoreEmployeeShift shift) => new()
    {
        Id = shift.Id,
        TenantId = shift.TenantId,
        StoreId = shift.StoreId,
        StaffId = shift.StaffId,
        ShiftDate = shift.ShiftDate,
        StartTime = shift.StartTime,
        EndTime = shift.EndTime,
        RoleType = shift.RoleType,
        Notes = shift.Notes,
        CreatedAt = shift.CreatedAt
    };

    /// <summary>
    /// 映射门店员工 DTO。
    /// </summary>
    /// <param name="staff">员工实体。</param>
    /// <returns>DTO。</returns>
    public static StoreStaffDto ToDto(MerchantStaff staff) => new()
    {
        Id = staff.Id,
        TenantId = staff.TenantId,
        MerchantId = staff.MerchantId,
        StoreId = staff.StoreId,
        Name = staff.Name,
        Phone = staff.Phone,
        Email = staff.Email,
        RoleType = staff.RoleType,
        Status = staff.Status
    };
}
