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

namespace SIGCM2.Application.Tests.Medios.GetById;

public class GetMedioByIdQueryHandlerTests
{
    private readonly IMedioRepository _repo = Substitute.For<IMedioRepository>();
    private readonly GetMedioByIdQueryHandler _handler;

    public GetMedioByIdQueryHandlerTests()
    {
        _handler = new GetMedioByIdQueryHandler(_repo);
    }

    private static Medio MakeMedio(int id) =>
        new(id, "COD" + id, "Nombre " + id, TipoMedio.Radio, 10, true, DateTime.UtcNow, null);

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

        await Assert.ThrowsAsync<MedioNotFoundException>(
            () => _handler.Handle(new GetMedioByIdQuery(999)));
    }

    [Fact]
    public async Task Handle_HappyPath_ReturnsDtoWithCorrectFields()
    {
        var medio = MakeMedio(3);
        _repo.GetByIdAsync(3, Arg.Any<CancellationToken>()).Returns(medio);

        var result = await _handler.Handle(new GetMedioByIdQuery(3));

        Assert.Equal(3, result.Id);
        Assert.Equal("COD3", result.Codigo);
        Assert.Equal("Nombre 3", result.Nombre);
        Assert.Equal(TipoMedio.Radio, result.Tipo);
        Assert.Equal(10, result.PlataformaEmpresaId);
        Assert.True(result.Activo);
    }
}
