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

namespace SIGCM2.Application.Tests.Secciones.GetById;

public class GetSeccionByIdQueryHandlerTests
{
    private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
    private readonly GetSeccionByIdQueryHandler _handler;

    public GetSeccionByIdQueryHandlerTests()
    {
        _handler = new GetSeccionByIdQueryHandler(_repo);
    }

    private static Seccion MakeSeccion(int id) =>
        new(id, 2, "COD" + id, "Nombre " + id, "suplementos", true, DateTime.UtcNow, null);

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

        await Assert.ThrowsAsync<SeccionNotFoundException>(
            () => _handler.Handle(new GetSeccionByIdQuery(999)));
    }

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

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

        Assert.Equal(5, result.Id);
        Assert.Equal(2, result.MedioId);
        Assert.Equal("COD5", result.Codigo);
        Assert.Equal("Nombre 5", result.Nombre);
        Assert.Equal("suplementos", result.Tipo);
        Assert.True(result.Activo);
    }
}
