using SIGCM2.Application.Common;
using SIGCM2.Domain.Fiscal;
using IibbEntity = SIGCM2.Domain.Entities.IngresosBrutos;

namespace SIGCM2.Application.Abstractions.Persistence;

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

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

    /// <summary>
    /// Updates cosmetic fields only (Descripcion, Activo).
    /// Never touches Alicuota, Provincia, or vigencia dates.
    /// </summary>
    Task<bool> UpdateCosmeticoAsync(int id, string descripcion, 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 Provincia filters.</summary>
    Task<PagedResult<IibbEntity>> ListAsync(IngresosBrutosQuery 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<IibbEntity>> GetHistorialAsync(int id, CancellationToken ct = default);
}
