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

namespace TakeoutSaaS.Application.App.Stores;

/// <summary>
/// 租户端门店列表映射助手。
/// </summary>
public static class StoreListMapping
{
    /// <summary>
    /// 映射租户端门店列表项。
    /// </summary>
    /// <param name="store">门店实体。</param>
    /// <returns>列表项 DTO。</returns>
    public static StoreListItemDto ToListItemDto(Store store) => new()
    {
        Id = store.Id,
        Name = store.Name,
        Code = store.Code,
        ContactPhone = store.Phone ?? string.Empty,
        ManagerName = store.ManagerName ?? string.Empty,
        Address = ResolveAddress(store),
        Province = store.Province,
        City = store.City,
        District = store.District,
        Longitude = store.Longitude,
        Latitude = store.Latitude,
        GeoStatus = store.GeoStatus,
        GeoFailReason = store.GeoFailReason,
        GeoUpdatedAt = store.GeoUpdatedAt,
        CoverImage = string.IsNullOrWhiteSpace(store.CoverImageUrl) ? store.SignboardImageUrl : store.CoverImageUrl,
        BusinessStatus = store.BusinessStatus,
        AuditStatus = store.AuditStatus,
        ServiceTypes = ResolveServiceTypes(store),
        CreatedAt = store.CreatedAt
    };

    /// <summary>
    /// 根据服务方式枚举映射门店开关字段。
    /// </summary>
    /// <param name="store">门店实体。</param>
    /// <param name="serviceTypes">服务方式集合。</param>
    public static void ApplyServiceTypes(Store store, IReadOnlyCollection<ServiceType> serviceTypes)
    {
        // 1. 默认关闭全部服务开关
        store.SupportsDelivery = false;
        store.SupportsPickup = false;
        store.SupportsDineIn = false;

        // 2. 按传入服务方式逐项启用
        foreach (var serviceType in serviceTypes)
        {
            switch (serviceType)
            {
                case ServiceType.Delivery:
                    store.SupportsDelivery = true;
                    break;
                case ServiceType.Pickup:
                    store.SupportsPickup = true;
                    break;
                case ServiceType.DineIn:
                    store.SupportsDineIn = true;
                    break;
            }
        }
    }

    /// <summary>
    /// 判断门店是否满足服务方式过滤。
    /// </summary>
    /// <param name="store">门店实体。</param>
    /// <param name="serviceType">服务方式。</param>
    /// <returns>满足返回 true。</returns>
    public static bool MatchServiceType(Store store, ServiceType serviceType) =>
        serviceType switch
        {
            ServiceType.Delivery => store.SupportsDelivery,
            ServiceType.Pickup => store.SupportsPickup,
            ServiceType.DineIn => store.SupportsDineIn,
            _ => false
        };

    private static IReadOnlyList<ServiceType> ResolveServiceTypes(Store store)
    {
        // 1. 聚合门店服务方式
        var result = new List<ServiceType>();
        if (store.SupportsDelivery)
        {
            result.Add(ServiceType.Delivery);
        }

        if (store.SupportsPickup)
        {
            result.Add(ServiceType.Pickup);
        }

        if (store.SupportsDineIn)
        {
            result.Add(ServiceType.DineIn);
        }

        return result;
    }

    private static string ResolveAddress(Store store)
    {
        return string.IsNullOrWhiteSpace(store.Address) ? string.Empty : store.Address;
    }
}

