using Dapper;
using Npgsql;

namespace Shared.Excel;

/// <summary>
/// Run daily from Hangfire. The default reference retention is 14 days.
/// Audit/domain records survive; raw uploaded files and staging rows do not.
/// </summary>
public sealed class PurgeExpiredImportsJob(NpgsqlDataSource dataSource)
{
    public async Task<int> RunAsync(CancellationToken ct)
    {
        await using var connection = await dataSource.OpenConnectionAsync(ct);
        return await connection.ExecuteAsync(new CommandDefinition("""
            delete from kbx.import_sessions
             where expires_at < now()
               and status in ('completed','partially-completed','failed','cancelled');
            """, cancellationToken: ct));
    }
}
