export type AutoScanStats = {
  clicked: number;
  attempted: number;
  verified: number;
  parsed: number;
  stored: number;
  review: number;
  duplicates: number;
  misses: number;
  pages: number;
};

export type ScanSummary = AutoScanStats & {
  mode: string;
  status: "done" | "stopped" | "blocked";
  targetCount?: number;
  gridLabel?: string;
};

export const emptyAutoScanStats: AutoScanStats = {
  clicked: 0,
  attempted: 0,
  verified: 0,
  parsed: 0,
  stored: 0,
  review: 0,
  duplicates: 0,
  misses: 0,
  pages: 0,
};

export function clampScanLimit(value: number) {
  return Math.max(1, Math.min(1800, Math.round(value || 1)));
}

export function clampSkipRows(value: number) {
  return Math.max(0, Math.min(8, Math.round(value || 0)));
}

export function resolveScanTargetCount(scanLimit: number, detectedInventoryCount?: number | null) {
  const detected = Number.isFinite(detectedInventoryCount) ? Number(detectedInventoryCount) : 0;
  const requested = clampScanLimit(scanLimit);
  if (detected > 0) return clampScanLimit(Math.min(requested, detected));
  return requested;
}
