import 'server-only';

export type ResetScope =
  | 'foundation_reset'
  | 'organization_reset'
  | 'business_reset'
  | 'demo_uat_reset'
  | 'full_reset'
  | 'csv_import_rollback'
  | 'setup_run_reset';

export type ReseedTarget = 'foundation' | 'demo_uat' | 'csv_manifest';

export interface ResetAffectedTable {
  table: string;
  estimatedRows: number;
  action: 'delete' | 'update_manifest' | 'report_only' | 'blocked';
  retainedData: string;
  deletedData: string;
}

export interface StorageCleanupPlan {
  mode: 'none' | 'best_effort' | 'blocked';
  plannedKeys: string[];
  warnings: string[];
}

export interface ResetPreviewReport {
  scope: ResetScope;
  targetOrganizationId: string | null;
  targetOrganizationCode: string | null;
  manifestId: string | null;
  summary: {
    tables: number;
    rows: number;
    warnings: number;
    errors: number;
  };
  affectedTables: ResetAffectedTable[];
  retainedData: string[];
  deletedData: string[];
  storageCleanup: StorageCleanupPlan;
  manifestImpact: Array<{
    id: string;
    name: string;
    version: string;
    status: string;
    action: 'none' | 'rolled_back' | 'warning' | 'deleted';
  }>;
  warnings: string[];
  errors: string[];
  confirmationPhrase: string;
  previewHash: string;
  confirmationToken: string;
  expiresAt: string;
}

export interface ResetExecuteReport extends ResetPreviewReport {
  executedAt: string;
  executed: boolean;
  deletedRows: number;
  updatedManifests: number;
  storageCleanupResult: {
    deletedKeys: string[];
    leftoverKeys: string[];
    warnings: string[];
  };
}

export interface ReseedPreviewReport {
  target: ReseedTarget;
  summary: {
    actions: number;
    warnings: number;
    errors: number;
  };
  actions: Array<{
    key: string;
    status: 'ready' | 'blocked' | 'warning';
    message: string;
  }>;
  warnings: string[];
  errors: string[];
  confirmationPhrase: string;
  previewHash: string;
  confirmationToken: string;
  expiresAt: string;
}

export interface ReseedExecuteReport extends ReseedPreviewReport {
  executedAt: string;
  executed: boolean;
}
