using Dapper;
using SIGCM2.Domain.Entities;
using SIGCM2.Infrastructure.Persistence;
using SIGCM2.TestSupport;

namespace SIGCM2.Application.Tests.Infrastructure;

/// <summary>
/// Integration tests for RefreshTokenRepository against SIGCM2_Test_App.
/// Uses shared SqlTestFixture via xUnit collection fixture; the repository opens its own
/// connections so transaction-scoped isolation would block on FK locks.
/// </summary>
[Collection("Database")]
public class RefreshTokenRepositoryTests : IAsyncLifetime
{
    private readonly SqlTestFixture _db;
    private RefreshTokenRepository _repository = null!;
    private int _testUserId;

    public RefreshTokenRepositoryTests(SqlTestFixture db)
    {
        _db = db;
    }

    public async Task InitializeAsync()
    {
        await _db.ResetAndSeedAsync();
        await SeedTestUserAsync();

        _testUserId = await _db.Connection.QuerySingleAsync<int>(
            "SELECT Id FROM dbo.Usuario WHERE Username = 'test_rt_user'");

        var factory = new SqlConnectionFactory(TestConnectionStrings.AppTestDb);
        _repository = new RefreshTokenRepository(factory);
    }

    public Task DisposeAsync() => Task.CompletedTask;

    private async Task SeedTestUserAsync()
    {
        await _db.Connection.ExecuteAsync("""
            SET QUOTED_IDENTIFIER ON;
            IF NOT EXISTS (SELECT 1 FROM dbo.Usuario WHERE Username = 'test_rt_user')
                INSERT INTO dbo.Usuario (Username, PasswordHash, Nombre, Apellido, Rol, PermisosJson, Activo)
                VALUES ('test_rt_user', '$2a$12$testhash', 'Test', 'User', 'admin', '["*"]', 1);
            """);
    }

    private static RefreshToken BuildToken(int usuarioId, string hash = "test_hash_abc123xyz", bool expired = false)
    {
        var now = DateTime.UtcNow;
        var ttl = expired ? TimeSpan.FromSeconds(1) : TimeSpan.FromDays(7);
        return RefreshToken.IssueForNewFamily(usuarioId, hash, now.AddHours(-1), ttl, "10.0.0.1", "TestAgent");
    }

    [Fact]
    public async Task AddAsync_PersistsAndReturnsId()
    {
        var token = BuildToken(_testUserId, "unique_hash_persist_" + Guid.NewGuid().ToString("N")[..8]);

        var id = await _repository.AddAsync(token);

        Assert.True(id > 0);
    }

    [Fact]
    public async Task AddAsync_DuplicateHash_Throws()
    {
        var hash = "duplicate_hash_" + Guid.NewGuid().ToString("N")[..8];
        var token1 = BuildToken(_testUserId, hash);
        var token2 = BuildToken(_testUserId, hash);

        await _repository.AddAsync(token1);

        await Assert.ThrowsAnyAsync<Exception>(() => _repository.AddAsync(token2));
    }

    [Fact]
    public async Task GetByHashAsync_RoundTripsAllFields()
    {
        var hash = "roundtrip_hash_" + Guid.NewGuid().ToString("N")[..8];
        var token = BuildToken(_testUserId, hash);

        await _repository.AddAsync(token);
        var retrieved = await _repository.GetByHashAsync(hash);

        Assert.NotNull(retrieved);
        Assert.Equal(_testUserId, retrieved.UsuarioId);
        Assert.Equal(hash, retrieved.TokenHash);
        Assert.Equal(token.FamilyId, retrieved.FamilyId);
        Assert.Null(retrieved.RevokedAt);
        Assert.Null(retrieved.ReplacedById);
    }

    [Fact]
    public async Task GetByHashAsync_NonExistentHash_ReturnsNull()
    {
        var result = await _repository.GetByHashAsync("does_not_exist_hash_abc");
        Assert.Null(result);
    }

    [Fact]
    public async Task RevokeAsync_SetsRevokedAtAndReplacedById()
    {
        var hash = "revoke_test_" + Guid.NewGuid().ToString("N")[..8];
        var token = BuildToken(_testUserId, hash);

        var id = await _repository.AddAsync(token);
        var revokedAt = DateTime.UtcNow;
        await _repository.RevokeAsync(id, replacedById: null, revokedAt: revokedAt);

        var retrieved = await _repository.GetByHashAsync(hash);

        Assert.NotNull(retrieved?.RevokedAt);
        Assert.Null(retrieved.ReplacedById);
    }

    [Fact]
    public async Task RevokeFamilyAsync_OnlyAffectsMatchingFamily()
    {
        var hash1 = "family_a_" + Guid.NewGuid().ToString("N")[..8];
        var hash2 = "family_b_" + Guid.NewGuid().ToString("N")[..8];

        var tokenA = BuildToken(_testUserId, hash1);
        var tokenB = BuildToken(_testUserId, hash2);

        await _repository.AddAsync(tokenA);
        await _repository.AddAsync(tokenB);

        var count = await _repository.RevokeFamilyAsync(tokenA.FamilyId, DateTime.UtcNow);

        Assert.Equal(1, count);

        var retrievedA = await _repository.GetByHashAsync(hash1);
        var retrievedB = await _repository.GetByHashAsync(hash2);

        Assert.NotNull(retrievedA?.RevokedAt);
        Assert.Null(retrievedB?.RevokedAt);
    }

    [Fact]
    public async Task RevokeAllActiveForUserAsync_DoesNotTouchAlreadyRevoked()
    {
        var hash1 = "user_active_" + Guid.NewGuid().ToString("N")[..8];
        var hash2 = "user_revoked_" + Guid.NewGuid().ToString("N")[..8];

        var tokenActive = BuildToken(_testUserId, hash1);
        var tokenAlreadyRevoked = BuildToken(_testUserId, hash2);

        var idActive = await _repository.AddAsync(tokenActive);
        var idRevoked = await _repository.AddAsync(tokenAlreadyRevoked);
        await _repository.RevokeAsync(idRevoked, null, DateTime.UtcNow.AddMinutes(-5));

        var count = await _repository.RevokeAllActiveForUserAsync(_testUserId, DateTime.UtcNow);

        Assert.Equal(1, count);

        var retrievedActive = await _repository.GetByHashAsync(hash1);
        Assert.NotNull(retrievedActive?.RevokedAt);
    }
}
