using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Pricing.ChargeableChars;
using SIGCM2.Domain.Pricing.ChargeableChars;

namespace SIGCM2.Application.Pricing.ChargeableChars.GetById;

/// <summary>
/// PRC-001 — Handler for GetChargeableCharConfigByIdQuery.
/// Returns null DTO when not found (API layer maps to 404).
/// </summary>
public sealed class GetChargeableCharConfigByIdQueryHandler
    : ICommandHandler<GetChargeableCharConfigByIdQuery, ChargeableCharConfigDto?>
{
    private readonly IChargeableCharConfigRepository _repo;

    public GetChargeableCharConfigByIdQueryHandler(IChargeableCharConfigRepository repo)
    {
        _repo = repo;
    }

    public async Task<ChargeableCharConfigDto?> Handle(GetChargeableCharConfigByIdQuery query)
    {
        var entity = await _repo.GetByIdAsync(query.Id);
        return entity is null ? null : ToDto(entity);
    }

    private static ChargeableCharConfigDto ToDto(ChargeableCharConfig c) => new(
        c.Id,
        c.ProductTypeId,
        c.Symbol,
        c.Category,
        c.PricePerUnit,
        c.ValidFrom,
        c.ValidTo,
        c.IsActive);
}
