namespace PowderCoating.Application.Interfaces;

/// <summary>
/// Orchestrates a full Columbia Coatings catalog sync: pull every product, map and upsert it, then
/// (only on a complete pull) reconcile discontinuations. Used by both the scheduled background job
/// and the manual "Sync now" admin action.
/// </summary>
public interface IColumbiaCatalogSyncService
{
    /// <summary>
    /// Runs one full sync. Assumes the caller has already decided it should run (enabled / due).
    /// Returns a result describing the outcome; never throws for an expected failure (not
    /// configured, partial pull, HTTP error) — those are reported on the result instead.
    /// </summary>
    Task<ColumbiaSyncResult> RunSyncAsync(CancellationToken cancellationToken = default);
}

/// <summary>Outcome of a Columbia catalog sync run.</summary>
public class ColumbiaSyncResult
{
    public bool Success { get; set; }
    public string? ErrorMessage { get; set; }

    public int TotalFetched { get; set; }
    public int Inserted { get; set; }
    public int Updated { get; set; }
    public int Unchanged { get; set; }
    public int Skipped { get; set; }
    public int Discontinued { get; set; }
    public int Reactivated { get; set; }

    public DateTime StartedAt { get; set; }
    public TimeSpan Duration { get; set; }

    /// <summary>One-line summary suitable for storing in the last-result platform setting / UI.</summary>
    public string Summary =>
        Success
            ? $"{TotalFetched} fetched: {Inserted} new, {Updated} updated, {Unchanged} unchanged, " +
              $"{Discontinued} discontinued, {Reactivated} reactivated ({Duration.TotalSeconds:F0}s)"
            : $"Failed: {ErrorMessage}";
}
