using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Common;
using SIGCM2.Application.Usuarios.Deactivate;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;

namespace SIGCM2.Application.Tests.Usuarios;

public class DeactivateUsuarioCommandHandlerTests
{
    private readonly IUsuarioRepository _repo = Substitute.For<IUsuarioRepository>();
    private readonly IRefreshTokenRepository _refreshRepo = Substitute.For<IRefreshTokenRepository>();
    private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
    private readonly DeactivateUsuarioCommandHandler _handler;

    public DeactivateUsuarioCommandHandlerTests()
    {
        _handler = new DeactivateUsuarioCommandHandler(_repo, _refreshRepo, _audit, TimeProvider.System);
        _repo.CountActiveAdminsAsync(Arg.Any<CancellationToken>()).Returns(2);
    }

    private static Usuario MakeUser(int id = 5, string rol = "cajero", bool activo = true)
        => new(id, "user" + id, "$2a$12$hash", "Test", "User", null, rol, "[]", activo);

    [Fact]
    public async Task Handle_Deactivates_Active_User_Returns_Activo_False()
    {
        var target = MakeUser(5, "cajero", true);
        var deactivated = MakeUser(5, "cajero", false);

        _repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(target);
        _repo.GetDetailAsync(5, Arg.Any<CancellationToken>()).Returns(deactivated);

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

        Assert.False(result.Activo);
        await _repo.Received(1).UpdateAsync(5, Arg.Any<UpdateUsuarioFields>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_Idempotent_When_Already_Inactive_No_FechaModificacion_Change()
    {
        var target = MakeUser(5, "cajero", false); // already inactive
        _repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(target);

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

        // Idempotent: should NOT call UpdateAsync
        await _repo.DidNotReceive().UpdateAsync(Arg.Any<int>(), Arg.Any<UpdateUsuarioFields>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>());
        Assert.False(result.Activo);
    }

    [Fact]
    public async Task Handle_Throws_LastAdminLockoutException_When_Last_Admin()
    {
        var lastAdmin = MakeUser(1, "admin", true);
        _repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(lastAdmin);
        _repo.CountActiveAdminsAsync(Arg.Any<CancellationToken>()).Returns(1);

        await Assert.ThrowsAsync<LastAdminLockoutException>(
            () => _handler.Handle(new DeactivateUsuarioCommand(1)));
    }

    [Fact]
    public async Task Handle_Revokes_Refresh_Tokens_When_Deactivating_Active_User()
    {
        var target = MakeUser(5, "cajero", true);
        var deactivated = MakeUser(5, "cajero", false);

        _repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(target);
        _repo.GetDetailAsync(5, Arg.Any<CancellationToken>()).Returns(deactivated);

        await _handler.Handle(new DeactivateUsuarioCommand(5));

        await _refreshRepo.Received(1).RevokeAllActiveForUserAsync(5, Arg.Any<DateTime>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_Does_NOT_Revoke_Tokens_When_Already_Inactive_Idempotent()
    {
        var target = MakeUser(5, "cajero", false); // already inactive
        _repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(target);

        await _handler.Handle(new DeactivateUsuarioCommand(5));

        await _refreshRepo.DidNotReceive().RevokeAllActiveForUserAsync(Arg.Any<int>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>());
    }

    [Fact]
    public async Task Handle_Throws_UsuarioNotFoundException_When_Not_Found()
    {
        _repo.GetByIdAsync(9999, Arg.Any<CancellationToken>()).Returns((Usuario?)null);

        await Assert.ThrowsAsync<UsuarioNotFoundException>(
            () => _handler.Handle(new DeactivateUsuarioCommand(9999)));
    }
}
