using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Common;
using SIGCM2.Application.Usuarios.List;
using SIGCM2.Domain.Exceptions;

namespace SIGCM2.Application.Tests.Usuarios;

public class ListUsuariosQueryHandlerTests
{
    private readonly IUsuarioRepository _repo = Substitute.For<IUsuarioRepository>();
    private readonly ListUsuariosQueryHandler _handler;

    public ListUsuariosQueryHandlerTests()
    {
        _handler = new ListUsuariosQueryHandler(_repo);
    }

    [Fact]
    public async Task Handle_Returns_PagedResult_With_Items()
    {
        var items = new List<UsuarioListItem>
        {
            new(1, "admin", "Admin", "Sys", null, "admin", true, null, null)
        };
        var paged = new PagedResult<UsuarioListItem>(items, 1, 20, 1);

        _repo.GetPagedAsync(Arg.Any<UsuariosQuery>(), Arg.Any<CancellationToken>())
            .Returns(paged);

        var query = new ListUsuariosQuery(1, 20, null, null, null);
        var result = await _handler.Handle(query);

        Assert.Equal(1, result.Total);
        Assert.Single(result.Items);
    }

    [Fact]
    public async Task Handle_Clamps_PageSize_Above_100_To_100()
    {
        _repo.GetPagedAsync(Arg.Any<UsuariosQuery>(), Arg.Any<CancellationToken>())
            .Returns(new PagedResult<UsuarioListItem>([], 1, 100, 0));

        var query = new ListUsuariosQuery(1, 200, null, null, null);
        await _handler.Handle(query);

        await _repo.Received(1).GetPagedAsync(
            Arg.Is<UsuariosQuery>(q => q.PageSize == 100),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_Clamps_Page_Below_1_To_1()
    {
        _repo.GetPagedAsync(Arg.Any<UsuariosQuery>(), Arg.Any<CancellationToken>())
            .Returns(new PagedResult<UsuarioListItem>([], 1, 20, 0));

        var query = new ListUsuariosQuery(0, 20, null, null, null);
        await _handler.Handle(query);

        await _repo.Received(1).GetPagedAsync(
            Arg.Is<UsuariosQuery>(q => q.Page == 1),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_Passes_Rol_Filter()
    {
        _repo.GetPagedAsync(Arg.Any<UsuariosQuery>(), Arg.Any<CancellationToken>())
            .Returns(new PagedResult<UsuarioListItem>([], 1, 20, 0));

        var query = new ListUsuariosQuery(1, 20, "admin", null, null);
        await _handler.Handle(query);

        await _repo.Received(1).GetPagedAsync(
            Arg.Is<UsuariosQuery>(q => q.Rol == "admin"),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_Passes_Activo_Filter()
    {
        _repo.GetPagedAsync(Arg.Any<UsuariosQuery>(), Arg.Any<CancellationToken>())
            .Returns(new PagedResult<UsuarioListItem>([], 1, 20, 0));

        var query = new ListUsuariosQuery(1, 20, null, false, null);
        await _handler.Handle(query);

        await _repo.Received(1).GetPagedAsync(
            Arg.Is<UsuariosQuery>(q => q.Activo == false),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_Passes_Search_Filter()
    {
        _repo.GetPagedAsync(Arg.Any<UsuariosQuery>(), Arg.Any<CancellationToken>())
            .Returns(new PagedResult<UsuarioListItem>([], 1, 20, 0));

        var query = new ListUsuariosQuery(1, 20, null, null, "juan");
        await _handler.Handle(query);

        await _repo.Received(1).GetPagedAsync(
            Arg.Is<UsuariosQuery>(q => q.Search == "juan"),
            Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_Returns_Empty_When_No_Items()
    {
        _repo.GetPagedAsync(Arg.Any<UsuariosQuery>(), Arg.Any<CancellationToken>())
            .Returns(new PagedResult<UsuarioListItem>([], 1, 20, 0));

        var result = await _handler.Handle(new ListUsuariosQuery(1, 20, null, null, null));

        Assert.Equal(0, result.Total);
        Assert.Empty(result.Items);
    }
}
