import type { CsvPreviewError, CsvPreviewWarning } from './types';

export type ImportStatus = 'PASS' | 'WARNING' | 'FAIL';
export type ImportAction = 'create' | 'update' | 'skip' | 'error';

export interface ImportRowReport {
  row: number;
  action: ImportAction;
  businessKey: string | null;
  entityId?: string;
  message: string;
  warnings: CsvPreviewWarning[];
  errors: CsvPreviewError[];
}

export interface ImportFileReport {
  fileName: string;
  template: string;
  status: ImportStatus;
  imported: number;
  updated: number;
  skipped: number;
  errors: CsvPreviewError[];
  warnings: CsvPreviewWarning[];
  rows: ImportRowReport[];
}

export interface ImportReport {
  status: ImportStatus;
  generatedAt: string;
  previewHash: string;
  committedHash: string;
  dryRun: boolean;
  summary: {
    imported: number;
    updated: number;
    skipped: number;
    errors: number;
    warnings: number;
  };
  files: ImportFileReport[];
  errors: CsvPreviewError[];
  warnings: CsvPreviewWarning[];
}

export function createImportError(input: {
  file: string;
  row: number;
  column: string;
  code: string;
  message: string;
  suggestedFix: string;
}): CsvPreviewError {
  return input;
}

export function createImportWarning(input: {
  file: string;
  row: number;
  column: string;
  code: string;
  message: string;
  suggestedFix: string;
}): CsvPreviewWarning {
  return input;
}

export function buildImportReport(input: {
  previewHash: string;
  committedHash: string;
  dryRun: boolean;
  files: ImportFileReport[];
}): ImportReport {
  const errors = input.files.flatMap((file) => file.errors);
  const warnings = input.files.flatMap((file) => file.warnings);
  const summary = input.files.reduce(
    (accumulator, file) => ({
      imported: accumulator.imported + file.imported,
      updated: accumulator.updated + file.updated,
      skipped: accumulator.skipped + file.skipped,
      errors: accumulator.errors + file.errors.length,
      warnings: accumulator.warnings + file.warnings.length
    }),
    { imported: 0, updated: 0, skipped: 0, errors: 0, warnings: 0 }
  );

  return {
    status: errors.length > 0 ? 'FAIL' : warnings.length > 0 ? 'WARNING' : 'PASS',
    generatedAt: new Date().toISOString(),
    previewHash: input.previewHash,
    committedHash: input.committedHash,
    dryRun: input.dryRun,
    summary,
    files: input.files,
    errors,
    warnings
  };
}

export function summarizeFileRows(input: {
  fileName: string;
  template: string;
  rows: ImportRowReport[];
  warnings?: CsvPreviewWarning[];
}): ImportFileReport {
  const errors = input.rows.flatMap((row) => row.errors);
  const rowWarnings = input.rows.flatMap((row) => row.warnings);
  const warnings = [...rowWarnings, ...(input.warnings ?? [])];

  return {
    fileName: input.fileName,
    template: input.template,
    status: errors.length > 0 ? 'FAIL' : warnings.length > 0 ? 'WARNING' : 'PASS',
    imported: input.rows.filter((row) => row.action === 'create').length,
    updated: input.rows.filter((row) => row.action === 'update').length,
    skipped: input.rows.filter((row) => row.action === 'skip').length,
    errors,
    warnings,
    rows: input.rows
  };
}
