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

namespace SIGCM2.Application.Tests.Medios;

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

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

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

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

    public Task DisposeAsync() => Task.CompletedTask;

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

    [Fact]
    public async Task AddAsync_ThenGetById_ReturnsAllColumns()
    {
        var medio = Medio.ForCreation("DIARIO01", "Diario Uno", TipoMedio.Diario, null);

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

        Assert.NotNull(result);
        Assert.Equal(id, result!.Id);
        Assert.Equal("DIARIO01", result.Codigo);
        Assert.Equal("Diario Uno", result.Nombre);
        Assert.Equal(TipoMedio.Diario, result.Tipo);
        Assert.Null(result.PlataformaEmpresaId);
        Assert.True(result.Activo);
        Assert.True(result.FechaCreacion > DateTime.MinValue);
        Assert.Null(result.FechaModificacion);
    }

    [Fact]
    public async Task AddAsync_WithPlataformaEmpresaId_PersistsValue()
    {
        var medio = Medio.ForCreation("RADIO99", "Radio Test", TipoMedio.Radio, 42);

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

        Assert.NotNull(result);
        Assert.Equal(42, result!.PlataformaEmpresaId);
        Assert.Equal(TipoMedio.Radio, result.Tipo);
    }

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

        Assert.Null(result);
    }

    // ── ExistsByCodigoAsync ───────────────────────────────────────────────────

    [Fact]
    public async Task ExistsByCodigoAsync_AfterAdd_ReturnsTrue()
    {
        var medio = Medio.ForCreation("EXIST01", "Existe", TipoMedio.Web, null);
        await _repository.AddAsync(medio);

        var exists = await _repository.ExistsByCodigoAsync("EXIST01");

        Assert.True(exists);
    }

    [Fact]
    public async Task ExistsByCodigoAsync_NotAdded_ReturnsFalse()
    {
        var exists = await _repository.ExistsByCodigoAsync("NOEXISTE_XYZ");

        Assert.False(exists);
    }

    [Fact]
    public async Task ExistsByCodigoAsync_IsCaseSensitive_MatchesAsStored()
    {
        var medio = Medio.ForCreation("UPPER01", "Upper Medio", TipoMedio.Diario, null);
        await _repository.AddAsync(medio);

        // Stored as 'UPPER01'; searching lowercase should not match (SQL_Latin1 collation is CI
        // on most default installs, but the contract is: match as stored).
        var exactMatch = await _repository.ExistsByCodigoAsync("UPPER01");
        Assert.True(exactMatch);
    }

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

    [Fact]
    public async Task UpdateAsync_ThenQuery_ReflectsNewValues()
    {
        var id = await _repository.AddAsync(Medio.ForCreation("UPD01", "Original", TipoMedio.Diario, null));
        var original = await _repository.GetByIdAsync(id);

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

        var result = await _repository.GetByIdAsync(id);

        Assert.NotNull(result);
        Assert.Equal("Actualizado", result!.Nombre);
        Assert.Equal(TipoMedio.Radio, result.Tipo);
        Assert.Equal(7, result.PlataformaEmpresaId);
        Assert.NotNull(result.FechaModificacion);
    }

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

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

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

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

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

    [Fact]
    public async Task GetPagedAsync_FilterByActivo_ReturnsOnlyActiveWhenTrue()
    {
        var idActivo = await _repository.AddAsync(Medio.ForCreation("ACTV01", "Activo", TipoMedio.Diario, null));
        var idInact = await _repository.AddAsync(Medio.ForCreation("INACT01", "Inactivo", TipoMedio.Diario, null));

        // Deactivate second medio
        var inact = await _repository.GetByIdAsync(idInact);
        await _repository.UpdateAsync(inact!.WithActivo(false, DateTime.UtcNow));

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

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

    [Fact]
    public async Task GetPagedAsync_FilterByTipo_ReturnsOnlyMatchingTipo()
    {
        await _repository.AddAsync(Medio.ForCreation("RADIO01", "Radio Uno", TipoMedio.Radio, null));
        await _repository.AddAsync(Medio.ForCreation("WEB01", "Web Uno", TipoMedio.Web, null));

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

        Assert.All(result.Items, m => Assert.Equal(TipoMedio.Radio, m.Tipo));
        Assert.Contains(result.Items, m => m.Codigo == "RADIO01");
    }

    [Fact]
    public async Task GetPagedAsync_SearchByNombre_ReturnsMatches()
    {
        await _repository.AddAsync(Medio.ForCreation("SRCH01", "Buscable Nombre", TipoMedio.Diario, null));
        await _repository.AddAsync(Medio.ForCreation("SRCH02", "Otro Medio", TipoMedio.Diario, null));

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

        Assert.Single(result.Items, m => m.Codigo == "SRCH01");
    }

    [Fact]
    public async Task GetPagedAsync_PaginationClamping_PageSizeClampedTo1Min()
    {
        await _repository.AddAsync(Medio.ForCreation("PAG01", "Paginacion", TipoMedio.Diario, null));

        // pageSize=0 should clamp to 1
        var result = await _repository.GetPagedAsync(new(Page: 1, PageSize: 0, Activo: null, Tipo: null, Search: null));

        Assert.Equal(1, result.PageSize);
    }

    [Fact]
    public async Task GetPagedAsync_TotalCount_ReflectsAllMatchingRows()
    {
        await _repository.AddAsync(Medio.ForCreation("CNT01", "Contador 1", TipoMedio.Diario, null));
        await _repository.AddAsync(Medio.ForCreation("CNT02", "Contador 2", TipoMedio.Diario, null));
        await _repository.AddAsync(Medio.ForCreation("CNT03", "Contador 3", TipoMedio.Diario, null));

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

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