using SIGCM2.Application.Common;
using SIGCM2.Domain.Entities;

namespace SIGCM2.Application.Abstractions.Persistence;

/// <summary>
/// Persistence contract for TipoDeIva. Implemented by Dapper repo in Infrastructure.
/// </summary>
public interface ITipoDeIvaRepository
{
    /// <summary>Inserts a new TipoDeIva and returns the generated identity Id.</summary>
    Task<int> InsertAsync(TipoDeIva entity, CancellationToken ct = default);

    /// <summary>Returns the TipoDeIva with the given Id, or null if not found.</summary>
    Task<TipoDeIva?> GetByIdAsync(int id, CancellationToken ct = default);

    /// <summary>
    /// Updates cosmetic fields only (Codigo, Descripcion, AplicaIVA, Activo).
    /// Never touches Porcentaje or vigencia dates.
    /// </summary>
    Task<bool> UpdateCosmeticoAsync(int id, string codigo, string descripcion, bool aplicaIVA, bool activo, CancellationToken ct = default);

    /// <summary>
    /// Closes the vigencia of the predecessor: UPDATE SET VigenciaHasta = @vigenciaHasta
    /// WHERE Id = @id AND VigenciaHasta IS NULL (optimistic guard for race conditions).
    /// Returns true if one row was affected, false if the row was already closed (race detected).
    /// </summary>
    Task<bool> UpdateCierreVigenciaAsync(int id, DateOnly vigenciaHasta, CancellationToken ct = default);

    /// <summary>Sets Activo to the given value. Returns true if one row was affected.</summary>
    Task<bool> SetActivoAsync(int id, bool activo, CancellationToken ct = default);

    /// <summary>Returns a paged list applying optional Activo and Codigo filters.</summary>
    Task<PagedResult<TipoDeIva>> ListAsync(TiposDeIvaQuery query, CancellationToken ct = default);

    /// <summary>
    /// Returns the full version chain for the record identified by <paramref name="id"/>,
    /// ordered from root (no PredecesorId) to the requested Id (inclusive).
    /// Implemented via a recursive CTE in the concrete repository.
    /// </summary>
    Task<IReadOnlyList<TipoDeIva>> GetHistorialAsync(int id, CancellationToken ct = default);
}
