using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Domain.Exceptions;

namespace SIGCM2.Application.Products.GetById;

public sealed class GetProductByIdQueryHandler
    : ICommandHandler<GetProductByIdQuery, ProductDetailDto>
{
    private readonly IProductRepository _repo;

    public GetProductByIdQueryHandler(IProductRepository repo)
    {
        _repo = repo;
    }

    public async Task<ProductDetailDto> Handle(GetProductByIdQuery query)
    {
        var product = await _repo.GetByIdAsync(query.Id)
            ?? throw new ProductNotFoundException(query.Id);

        return new ProductDetailDto(
            product.Id, product.Nombre,
            product.MedioId, product.ProductTypeId, product.RubroId,
            product.BasePrice, product.PriceDurationDays,
            product.IsActive,
            product.FechaCreacion, product.FechaModificacion);
    }
}
