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

namespace TakeoutSaaS.Application.App.Products.Handlers;

/// <summary>
/// 创建商品命令处理器。
/// </summary>
public sealed class CreateProductCommandHandler(IProductRepository productRepository, ILogger<CreateProductCommandHandler> logger)
    : IRequestHandler<CreateProductCommand, ProductDto>
{
    private readonly IProductRepository _productRepository = productRepository;
    private readonly ILogger<CreateProductCommandHandler> _logger = logger;

    /// <inheritdoc />
    public async Task<ProductDto> Handle(CreateProductCommand request, CancellationToken cancellationToken)
    {
        // 1. 构建实体
        var product = new Product
        {
            StoreId = request.StoreId,
            CategoryId = request.CategoryId,
            SpuCode = request.SpuCode.Trim(),
            Name = request.Name.Trim(),
            Subtitle = request.Subtitle?.Trim(),
            Unit = request.Unit?.Trim(),
            Price = request.Price,
            OriginalPrice = request.OriginalPrice,
            StockQuantity = request.StockQuantity,
            MaxQuantityPerOrder = request.MaxQuantityPerOrder,
            Status = request.Status,
            CoverImage = request.CoverImage?.Trim(),
            GalleryImages = request.GalleryImages?.Trim(),
            Description = request.Description?.Trim(),
            EnableDineIn = request.EnableDineIn,
            EnablePickup = request.EnablePickup,
            EnableDelivery = request.EnableDelivery,
            IsFeatured = request.IsFeatured
        };

        // 2. 持久化
        await _productRepository.AddProductAsync(product, cancellationToken);
        await _productRepository.SaveChangesAsync(cancellationToken);
        _logger.LogInformation("创建商品 {ProductId} - {ProductName}", product.Id, product.Name);

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

    private static ProductDto MapToDto(Product product) => new()
    {
        Id = product.Id,
        TenantId = product.TenantId,
        StoreId = product.StoreId,
        CategoryId = product.CategoryId,
        SpuCode = product.SpuCode,
        Name = product.Name,
        Subtitle = product.Subtitle,
        Unit = product.Unit,
        Price = product.Price,
        OriginalPrice = product.OriginalPrice,
        StockQuantity = product.StockQuantity,
        MaxQuantityPerOrder = product.MaxQuantityPerOrder,
        Status = product.Status,
        CoverImage = product.CoverImage,
        GalleryImages = product.GalleryImages,
        Description = product.Description,
        EnableDineIn = product.EnableDineIn,
        EnablePickup = product.EnablePickup,
        EnableDelivery = product.EnableDelivery,
        IsFeatured = product.IsFeatured,
        CreatedAt = product.CreatedAt
    };
}
