using MediatR;
using Microsoft.Extensions.Logging;
using TakeoutSaaS.Application.App.Stores.Commands;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Domain.Stores.Entities;
using TakeoutSaaS.Domain.Stores.Repositories;

namespace TakeoutSaaS.Application.App.Stores.Handlers;

/// <summary>
/// 创建门店命令处理器。
/// </summary>
public sealed class CreateStoreCommandHandler(IStoreRepository storeRepository, ILogger<CreateStoreCommandHandler> logger)
    : IRequestHandler<CreateStoreCommand, StoreDto>
{
    private readonly IStoreRepository _storeRepository = storeRepository;
    private readonly ILogger<CreateStoreCommandHandler> _logger = logger;

    /// <inheritdoc />
    public async Task<StoreDto> Handle(CreateStoreCommand request, CancellationToken cancellationToken)
    {
        // 1. 构建实体
        var store = new Store
        {
            MerchantId = request.MerchantId,
            Code = request.Code.Trim(),
            Name = request.Name.Trim(),
            Phone = request.Phone?.Trim(),
            ManagerName = request.ManagerName?.Trim(),
            Status = request.Status,
            Province = request.Province?.Trim(),
            City = request.City?.Trim(),
            District = request.District?.Trim(),
            Address = request.Address?.Trim(),
            Longitude = request.Longitude,
            Latitude = request.Latitude,
            Announcement = request.Announcement?.Trim(),
            Tags = request.Tags?.Trim(),
            DeliveryRadiusKm = request.DeliveryRadiusKm,
            SupportsDineIn = request.SupportsDineIn,
            SupportsPickup = request.SupportsPickup,
            SupportsDelivery = request.SupportsDelivery,
            SupportsReservation = request.SupportsReservation,
            SupportsQueueing = request.SupportsQueueing
        };

        // 2. 持久化
        await _storeRepository.AddStoreAsync(store, cancellationToken);
        await _storeRepository.SaveChangesAsync(cancellationToken);
        _logger.LogInformation("创建门店 {StoreId} - {StoreName}", store.Id, store.Name);

        // 3. 返回 DTO
        return MapToDto(store);
    }

    private static StoreDto MapToDto(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,
        Province = store.Province,
        City = store.City,
        District = store.District,
        Address = store.Address,
        Longitude = store.Longitude,
        Latitude = store.Latitude,
        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
    };
}
