export type SetupCheckStatus = "PASS" | "WARNING" | "FAIL";

export interface SetupCheckResult {
  id: string;
  area: string;
  name: string;
  status: SetupCheckStatus;
  requiredFor: string[];
  purpose: string;
  message: string;
  suggestedFix?: string;
  details?: Record<string, unknown>;
  checkedAt: string;
}

export interface SetupReportSummary {
  pass: number;
  warning: number;
  fail: number;
  total: number;
}

export interface SetupReadinessReport {
  success: boolean;
  time: string;
  status: SetupCheckStatus;
  message: string;
  summary: SetupReportSummary;
  checks: SetupCheckResult[];
}

export interface SetupVerificationPayload {
  organizationId?: string;
  administratorEmail?: string;
  profileId?: string;
}

export interface SetupVerificationReport extends SetupReadinessReport {
  organizationId: string | null;
}

export interface InstallationProfileDto {
  id: string;
  name: string;
  description: string;
  wizardSteps: string[];
  requiredTemplates: string[];
  verificationChecks: string[];
  resetScopes: string[];
  reseedTargets: string[];
  defaultOptions: Record<string, unknown>;
  enabledPluginIds: string[];
}

export interface SetupPluginSummaryDto {
  id: string;
  version: string;
  displayName: string;
  description: string;
  dependencies: string[];
}

export interface SetupWizardStepDto {
  key: string;
  title: string;
  description: string;
  required: boolean;
  produces: string[];
  invalidates: string[];
}

export interface SetupRegistryDefinitionDto {
  key?: string;
  id?: string;
  area?: string;
  name?: string;
  description?: string;
}

export interface SetupTemplateMetadataItem {
  file: string;
  seedType: string;
  importOrder: number;
  supported: boolean;
  requiredColumns: string[];
  description: string;
  plugin?: string;
  profiles?: string[];
  visible?: boolean;
}

export interface SetupImportSourceFileDto {
  fileName: string;
  kind: 'required' | 'optional';
  bytes: number;
  modifiedAt: string;
  checksum: string;
  headers: string[];
}

export interface SetupImportIgnoredFileDto {
  fileName: string;
  reason: string;
}

export interface SetupImportSourceDto {
  envKey: string;
  exampleValue: string;
  configuredValue: string | null;
  configured: boolean;
  resolvedPath: string | null;
  exists: boolean;
  files: SetupImportSourceFileDto[];
  missingRequiredFiles: string[];
  ignoredFiles: SetupImportIgnoredFileDto[];
  errors: string[];
  warnings: string[];
}

export interface SetupTemplatesResponse {
  success: boolean;
  time: string;
  version: string;
  generatedFrom: string;
  profile?: InstallationProfileDto | null;
  plugins?: SetupPluginSummaryDto[];
  templates: SetupTemplateMetadataItem[];
}

export interface BootstrapRequiredStepDto {
  stepKey: string;
  fileName: string;
  title: string;
  description: string;
  dependsOn: string[];
}

export interface BootstrapSetupStatusResponse {
  initialized: boolean;
  tokenRequired: boolean;
  checks: {
    completedSetupRun: boolean;
    superAdminUser: boolean;
    organization: boolean;
    adminMembership: boolean;
  };
  requiredFiles: string[];
  optionalFiles: string[];
  requiredSteps: BootstrapRequiredStepDto[];
  importSource: SetupImportSourceDto;
}

export interface SetupProfilesResponse {
  success: boolean;
  time: string;
  selectedProfile: InstallationProfileDto | null;
  profiles: InstallationProfileDto[];
  plugins: SetupPluginSummaryDto[];
  templates: SetupTemplateMetadataItem[];
  wizardSteps: SetupWizardStepDto[];
  readinessChecks: SetupRegistryDefinitionDto[];
  verificationChecks: SetupRegistryDefinitionDto[];
  resetScopes: SetupRegistryDefinitionDto[];
  reseedTargets: SetupRegistryDefinitionDto[];
  goldenDatasets: SetupRegistryDefinitionDto[];
}

export type SetupRunStatus =
  | "not_started"
  | "readiness_checked"
  | "foundation_installed"
  | "organization_created"
  | "administrator_created"
  | "scope_configured"
  | "sequences_configured"
  | "approval_configured"
  | "csv_validated"
  | "csv_imported"
  | "verified"
  | "completed"
  | "failed"
  | "cancelled";

export type SetupStepStatus =
  | "not_started"
  | "running"
  | "passed"
  | "warning"
  | "failed"
  | "skipped"
  | "stale";

export interface SetupRunDto {
  id: string;
  status: SetupRunStatus;
  mode: string;
  targetOrganizationId: string | null;
  startedBy: string;
  completedBy: string | null;
  startedAt: string;
  completedAt: string | null;
  lastError: string | null;
  metadata: Record<string, unknown> | null;
  createdAt: string;
  updatedAt: string;
}

export interface SetupRunStepDto {
  id: string;
  setupRunId: string;
  stepKey: string;
  status: SetupStepStatus;
  inputHash: string | null;
  outputJson: Record<string, unknown> | null;
  errorsJson: unknown[] | null;
  warningsJson: unknown[] | null;
  startedAt: string | null;
  completedAt: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface SeedManifestItemDto {
  id: string;
  seedManifestId: string;
  itemKey: string;
  sourceFile: string;
  checksum: string;
  status: string;
  importedCount: number;
  updatedCount: number;
  skippedCount: number;
  errorCount: number;
  warningCount: number;
  reportJson: Record<string, unknown> | null;
  createdAt: string;
  updatedAt: string;
}

export interface SeedManifestDto {
  id: string;
  setupRunId: string | null;
  organizationId: string | null;
  name: string;
  version: string;
  type: string;
  checksum: string;
  status: string;
  source: string;
  appliedBy: string | null;
  appliedAt: string | null;
  reportJson: Record<string, unknown> | null;
  createdAt: string;
  updatedAt: string;
}

export interface SetupRunResponse {
  active: boolean;
  completed: boolean;
  run: SetupRunDto | null;
  steps: SetupRunStepDto[];
  manifests: SeedManifestDto[];
  manifestItems: SeedManifestItemDto[];
}

export interface SaveSetupStepPayload {
  setupRunId?: string;
  stepKey: string;
  status: SetupStepStatus;
  inputHash?: string | null;
  output?: Record<string, unknown> | null;
  errors?: unknown[] | null;
  warnings?: unknown[] | null;
}

export interface CompleteBootstrapSetupPayload {
  report?: Record<string, unknown>;
  bootstrapToken?: string;
}

export type {
  CsvPreviewError,
  CsvPreviewFileResult,
  CsvPreviewRow,
  CsvPreviewResult,
  CsvPreviewStatus,
  CsvPreviewWarning,
} from "@/features/setup/server/csv/types";

export type {
  ImportFileReport,
  ImportReport,
  ImportRowReport,
  ImportStatus,
} from "@/features/setup/server/csv/import-report";
