using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.PuntosDeVenta.GetById;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;

namespace SIGCM2.Application.Tests.PuntosDeVenta.GetById;

public class GetPuntoDeVentaByIdQueryHandlerTests
{
    private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
    private readonly GetPuntoDeVentaByIdQueryHandler _handler;

    public GetPuntoDeVentaByIdQueryHandlerTests()
    {
        _handler = new GetPuntoDeVentaByIdQueryHandler(_repo);
    }

    private static PuntoDeVenta MakePdv(int id = 5) =>
        new(id, 2, 3, "PdV " + id, "Desc", true, DateTime.UtcNow, null);

    [Fact]
    public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
    {
        _repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);

        await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
            () => _handler.Handle(new GetPuntoDeVentaByIdQuery(999)));
    }

    [Fact]
    public async Task Handle_HappyPath_ReturnsDtoWithCorrectFields()
    {
        var pdv = MakePdv(5);
        _repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(pdv);

        var result = await _handler.Handle(new GetPuntoDeVentaByIdQuery(5));

        Assert.Equal(5, result.Id);
        Assert.Equal(2, result.MedioId);
        Assert.Equal(3, result.NumeroAFIP);
        Assert.Equal("PdV 5", result.Nombre);
        Assert.Equal("Desc", result.Descripcion);
        Assert.True(result.Activo);
    }
}
