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

namespace TakeoutSaaS.Application.App.Stores.Handlers;

/// <summary>
/// 删除节假日配置处理器。
/// </summary>
public sealed class DeleteStoreHolidayCommandHandler(
    IStoreRepository storeRepository,
    ILogger<DeleteStoreHolidayCommandHandler> logger)
    : IRequestHandler<DeleteStoreHolidayCommand, bool>
{
    /// <inheritdoc />
    public async Task<bool> Handle(DeleteStoreHolidayCommand request, CancellationToken cancellationToken)
    {
        // 1. 读取配置
        var existing = await storeRepository.FindHolidayByIdAsync(request.HolidayId, null, cancellationToken);
        if (existing is null)
        {
            return false;
        }

        // 2. 校验门店归属
        if (existing.StoreId != request.StoreId)
        {
            return false;
        }

        // 3. 删除
        await storeRepository.DeleteHolidayAsync(request.HolidayId, null, cancellationToken);
        await storeRepository.SaveChangesAsync(cancellationToken);
        logger.LogInformation("删除节假日 {HolidayId} 对应门店 {StoreId}", request.HolidayId, request.StoreId);

        return true;
    }
}
