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

namespace SIGCM2.Application.ProductTypes.GetById;

public sealed class GetProductTypeByIdQueryHandler
    : ICommandHandler<GetProductTypeByIdQuery, ProductTypeDetailDto>
{
    private readonly IProductTypeRepository _repo;

    public GetProductTypeByIdQueryHandler(IProductTypeRepository repo)
    {
        _repo = repo;
    }

    public async Task<ProductTypeDetailDto> Handle(GetProductTypeByIdQuery query)
    {
        var pt = await _repo.GetByIdAsync(query.Id)
            ?? throw new ProductTypeNotFoundException(query.Id);

        return new ProductTypeDetailDto(
            pt.Id, pt.Nombre,
            pt.HasDuration, pt.RequiresText, pt.RequiresCategory, pt.IsBundle,
            pt.AllowImages,
            pt.MaxImages, pt.MaxImageSizeMB, pt.MaxImageWidth, pt.MaxImageHeight,
            pt.IsActive,
            pt.FechaCreacion, pt.FechaModificacion);
    }
}
