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

namespace SIGCM2.Application.Tests.Secciones;

/// <summary>
/// Integration tests for SeccionRepository against SIGCM2_Test_App.
/// TDD: RED written before implementation, GREEN after SeccionRepository was created.
/// Temporal: after UpdateAsync, dbo.Seccion_History MUST have ≥1 row for that Id.
/// </summary>
[Collection("Database")]
public class SeccionRepositoryTests : IAsyncLifetime
{
    private readonly SqlTestFixture _db;
    private SeccionRepository _repository = null!;
    private MedioRepository _medioRepository = null!;
    private int _medioId;

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

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

        var factory = new SqlConnectionFactory(TestConnectionStrings.AppTestDb);
        _repository = new SeccionRepository(factory);
        _medioRepository = new MedioRepository(factory);

        // Seed a canonical Medio for FK-valid Seccion tests.
        _medioId = await _medioRepository.AddAsync(Medio.ForCreation("TESTMEDIO", "Medio de Prueba", TipoMedio.Diario, null));
    }

    public Task DisposeAsync() => Task.CompletedTask;

    // ── AddAsync + GetByIdAsync roundtrip ─────────────────────────────────────

    [Fact]
    public async Task AddAsync_ThenGetById_ReturnsAllColumns()
    {
        var seccion = Seccion.ForCreation(_medioId, "SEC01", "Sección Uno", "clasificados");

        var id = await _repository.AddAsync(seccion);
        var result = await _repository.GetByIdAsync(id);

        Assert.NotNull(result);
        Assert.Equal(id, result!.Id);
        Assert.Equal(_medioId, result.MedioId);
        Assert.Equal("SEC01", result.Codigo);
        Assert.Equal("Sección Uno", result.Nombre);
        Assert.Equal("clasificados", result.Tipo);
        Assert.True(result.Activo);
        Assert.True(result.FechaCreacion > DateTime.MinValue);
        Assert.Null(result.FechaModificacion);
    }

    [Fact]
    public async Task GetByIdAsync_NonExistent_ReturnsNull()
    {
        var result = await _repository.GetByIdAsync(999999);

        Assert.Null(result);
    }

    // ── FK violation ──────────────────────────────────────────────────────────

    [Fact]
    public async Task AddAsync_WithInvalidMedioId_ThrowsSqlException()
    {
        var seccion = Seccion.ForCreation(99999, "FKERR01", "FK Error", "notables");

        await Assert.ThrowsAsync<Microsoft.Data.SqlClient.SqlException>(
            () => _repository.AddAsync(seccion));
    }

    // ── ExistsByCodigoInMedioAsync ─────────────────────────────────────────────

    [Fact]
    public async Task ExistsByCodigoInMedioAsync_AfterAdd_ReturnsTrue()
    {
        await _repository.AddAsync(Seccion.ForCreation(_medioId, "EXIST01", "Existe", "clasificados"));

        var exists = await _repository.ExistsByCodigoInMedioAsync(_medioId, "EXIST01");

        Assert.True(exists);
    }

    [Fact]
    public async Task ExistsByCodigoInMedioAsync_NotAdded_ReturnsFalse()
    {
        var exists = await _repository.ExistsByCodigoInMedioAsync(_medioId, "NOEXISTE_XYZ");

        Assert.False(exists);
    }

    [Fact]
    public async Task ExistsByCodigoInMedioAsync_SameCodigoDifferentMedio_ReturnsFalse()
    {
        // Seccion with same Codigo exists for _medioId, but NOT for a different medioId.
        await _repository.AddAsync(Seccion.ForCreation(_medioId, "SHARED01", "Shared Codigo", "suplementos"));

        var otherMedioId = await _medioRepository.AddAsync(Medio.ForCreation("OTRO01", "Otro Medio", TipoMedio.Radio, null));
        var exists = await _repository.ExistsByCodigoInMedioAsync(otherMedioId, "SHARED01");

        Assert.False(exists);
    }

    // ── UpdateAsync + Temporal ────────────────────────────────────────────────

    [Fact]
    public async Task UpdateAsync_ThenQuery_ReflectsNewValues()
    {
        var id = await _repository.AddAsync(Seccion.ForCreation(_medioId, "UPD01", "Original", "clasificados"));
        var original = await _repository.GetByIdAsync(id);

        var updated = original!.WithUpdatedProfile("Actualizado", "notables", DateTime.UtcNow);
        await _repository.UpdateAsync(updated);

        var result = await _repository.GetByIdAsync(id);

        Assert.NotNull(result);
        Assert.Equal("Actualizado", result!.Nombre);
        Assert.Equal("notables", result.Tipo);
        Assert.NotNull(result.FechaModificacion);
    }

    [Fact]
    public async Task UpdateAsync_ProducesHistoryRow()
    {
        // Temporal: SQL Server automatically writes the previous row version to Seccion_History on UPDATE.
        var id = await _repository.AddAsync(Seccion.ForCreation(_medioId, "HIST01", "Historial", "clasificados"));
        var original = await _repository.GetByIdAsync(id);

        var updated = original!.WithUpdatedProfile("Historial v2", "suplementos", DateTime.UtcNow);
        await _repository.UpdateAsync(updated);

        var historyCount = await _db.Connection.ExecuteScalarAsync<int>(
            "SELECT COUNT(*) FROM dbo.Seccion_History WHERE Id = @Id", new { Id = id });

        Assert.True(historyCount >= 1, $"Expected ≥1 history row for Seccion Id={id}, got {historyCount}");
    }

    // ── GetPagedAsync ─────────────────────────────────────────────────────────

    [Fact]
    public async Task GetPagedAsync_FilterByMedioId_ReturnsOnlySecciones_OfThatMedio()
    {
        var otherMedioId = await _medioRepository.AddAsync(Medio.ForCreation("OTHER02", "Otro Medio 2", TipoMedio.Radio, null));

        await _repository.AddAsync(Seccion.ForCreation(_medioId, "M1S01", "M1 Sec 1", "clasificados"));
        await _repository.AddAsync(Seccion.ForCreation(otherMedioId, "M2S01", "M2 Sec 1", "notables"));

        var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, MedioId: _medioId, Tipo: null, Activo: null, Search: null));

        Assert.All(result.Items, s => Assert.Equal(_medioId, s.MedioId));
        Assert.Contains(result.Items, s => s.Codigo == "M1S01");
        Assert.DoesNotContain(result.Items, s => s.Codigo == "M2S01");
    }

    [Fact]
    public async Task GetPagedAsync_FilterByTipo_ReturnsOnlyMatchingTipo()
    {
        await _repository.AddAsync(Seccion.ForCreation(_medioId, "CL01", "Clasificados", "clasificados"));
        await _repository.AddAsync(Seccion.ForCreation(_medioId, "NT01", "Notables", "notables"));

        var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, MedioId: null, Tipo: "clasificados", Activo: null, Search: null));

        Assert.All(result.Items, s => Assert.Equal("clasificados", s.Tipo));
        Assert.Contains(result.Items, s => s.Codigo == "CL01");
    }

    [Fact]
    public async Task GetPagedAsync_FilterByActivo_ReturnsOnlyActive()
    {
        await _repository.AddAsync(Seccion.ForCreation(_medioId, "ACTV01", "Activa", "clasificados"));
        var inactId = await _repository.AddAsync(Seccion.ForCreation(_medioId, "INACT01", "Inactiva", "clasificados"));

        var inact = await _repository.GetByIdAsync(inactId);
        await _repository.UpdateAsync(inact!.WithActivo(false, DateTime.UtcNow));

        var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 50, MedioId: _medioId, Tipo: null, Activo: true, Search: null));

        var codigos = result.Items.Select(s => s.Codigo).ToHashSet();
        Assert.Contains("ACTV01", codigos);
        Assert.DoesNotContain("INACT01", codigos);
    }

    [Fact]
    public async Task GetPagedAsync_TotalCount_ReflectsAllMatchingRows()
    {
        await _repository.AddAsync(Seccion.ForCreation(_medioId, "P01", "Page 1", "suplementos"));
        await _repository.AddAsync(Seccion.ForCreation(_medioId, "P02", "Page 2", "suplementos"));
        await _repository.AddAsync(Seccion.ForCreation(_medioId, "P03", "Page 3", "suplementos"));

        var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 2, MedioId: _medioId, Tipo: "suplementos", Activo: null, Search: null));

        Assert.Equal(3, result.Total);
        Assert.Equal(2, result.Items.Count);
    }
}
