import { SyncRunStatus } from "@prisma/client";
import type { SyncStats } from "@/lib/licitations/types";

export function resolveSyncStatus(stats: SyncStats, warnings: string[] = []): SyncRunStatus {
  if (stats.errors > 0) {
    return stats.inserted > 0 || stats.updated > 0 ? SyncRunStatus.PARTIAL : SyncRunStatus.FAILED;
  }

  if (stats.totalFetched === 0 && warnings.length > 0) {
    return SyncRunStatus.PARTIAL;
  }

  return SyncRunStatus.SUCCESS;
}

export function shouldAdvanceOpenSyncTimestamp(status: SyncRunStatus, stats: SyncStats) {
  const hasMeaningfulProgress = stats.totalFetched > 0 || stats.inserted > 0 || stats.updated > 0;
  return status !== SyncRunStatus.FAILED && hasMeaningfulProgress;
}
