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;
using TakeoutSaaS.Shared.Abstractions.Tenancy;

namespace TakeoutSaaS.Application.App.Stores.Handlers;

/// <summary>
/// 更新门店命令处理器。
/// </summary>
public sealed class UpdateStoreCommandHandler(
    IStoreRepository storeRepository,
    ITenantProvider tenantProvider,
    ILogger<UpdateStoreCommandHandler> logger)
    : IRequestHandler<UpdateStoreCommand, StoreDto?>
{
    private readonly IStoreRepository _storeRepository = storeRepository;
    private readonly ITenantProvider _tenantProvider = tenantProvider;
    private readonly ILogger<UpdateStoreCommandHandler> _logger = logger;

    /// <inheritdoc />
    public async Task<StoreDto?> Handle(UpdateStoreCommand request, CancellationToken cancellationToken)
    {
        // 1. 读取门店
        var tenantId = _tenantProvider.GetCurrentTenantId();
        var existing = await _storeRepository.FindByIdAsync(request.StoreId, tenantId, cancellationToken);
        if (existing == null)
        {
            return null;
        }

        // 2. 更新字段
        existing.MerchantId = request.MerchantId;
        existing.Code = request.Code.Trim();
        existing.Name = request.Name.Trim();
        existing.Phone = request.Phone?.Trim();
        existing.ManagerName = request.ManagerName?.Trim();
        existing.Status = request.Status;
        existing.Province = request.Province?.Trim();
        existing.City = request.City?.Trim();
        existing.District = request.District?.Trim();
        existing.Address = request.Address?.Trim();
        existing.Longitude = request.Longitude;
        existing.Latitude = request.Latitude;
        existing.Announcement = request.Announcement?.Trim();
        existing.Tags = request.Tags?.Trim();
        existing.DeliveryRadiusKm = request.DeliveryRadiusKm;
        existing.SupportsDineIn = request.SupportsDineIn;
        existing.SupportsPickup = request.SupportsPickup;
        existing.SupportsDelivery = request.SupportsDelivery;
        existing.SupportsReservation = request.SupportsReservation;
        existing.SupportsQueueing = request.SupportsQueueing;

        // 3. 持久化
        await _storeRepository.UpdateStoreAsync(existing, cancellationToken);
        await _storeRepository.SaveChangesAsync(cancellationToken);
        _logger.LogInformation("更新门店 {StoreId} - {StoreName}", existing.Id, existing.Name);

        // 4. 返回 DTO
        return MapToDto(existing);
    }

    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
    };
}
