/**
 * types.ts — Zod input schema + internal interfaces for preflight-develop-plan CLI.
 *
 * Validates that the development plan is executable: all modules have PRD files,
 * audit verdicts, and are dev-ready (GO, score ≥ 80).
 */
import { z } from 'zod';

// ---------------------------------------------------------------------------
// Input schema (Zod)
// ---------------------------------------------------------------------------

export const PreflightInputSchema = z.object({
  /** Absolute path to the .smartstack/ba/ directory. */
  baRoot: z.string().min(1),

  /** Absolute path to the target project (where generated code lands). */
  projectPath: z.string().min(1),

  /**
   * Optional: only preflight specific waves.
   * If omitted, all waves are checked.
   */
  waves: z.array(z.number().int().min(1)).optional(),
});

export type PreflightInput = z.infer<typeof PreflightInputSchema>;

// ---------------------------------------------------------------------------
// Plan structures (read from dev-plan.json — matches create-plan-development types)
// ---------------------------------------------------------------------------

export interface ReadinessInfo {
  verdict: 'GO' | 'NO-GO' | 'UNKNOWN';
  score?: number;
  errCount?: number;
  warnCount?: number;
}

export interface WaveModule {
  appCode: string;
  moduleCode: string;
  key: string; // APP/MODULE
  entityCount: number;
  dependsOn: string[];
  readiness?: ReadinessInfo;
  isExternal: boolean;
  referencedBy?: string[];
  warnings: string[];
}

export interface Wave {
  level: number;
  modules: WaveModule[];
}

export interface DevelopmentPlan {
  generatedAt: string;
  apps: string[];
  totalModules: number;
  totalCrossModuleDeps: number;
  waves: Wave[];
  cycles: string[][];
  warnings: string[];
}

// ---------------------------------------------------------------------------
// Preflight result
// ---------------------------------------------------------------------------

export type ModuleStatus = 'ready' | 'not-ready' | 'external';

export interface ModulePreflight {
  appCode: string;
  moduleCode: string;
  key: string;
  wave: number;
  status: ModuleStatus;
  /** Directory: .smartstack/ba/<APP>/<MODULE> */
  moduleDir: string;
  /** PRD dev-readiness */
  readiness: ReadinessInfo;
  /** Which PRD files are present */
  prdFiles: {
    prd: boolean;
    entities: boolean;
    api: boolean;
    frontend: boolean;
    pagespecs: boolean;
  };
  /** Blocking issues for this module */
  blockers: string[];
  /** Non-blocking issues */
  warnings: string[];
}

export interface WavePreflight {
  level: number;
  status: 'ready' | 'blocked';
  modules: ModulePreflight[];
  /** Modules in this wave that block execution */
  blockers: string[];
}

export interface PreflightReport {
  planDate: string;
  apps: string[];
  projectPath: string;
  totalModules: number;
  readyModules: number;
  blockedModules: number;
  externalDeps: number;
  hasCycles: boolean;
  waves: WavePreflight[];
  /** Overall: can we proceed? */
  canProceed: boolean;
  /** First wave that is fully ready (all modules ready) */
  firstReadyWave: number | null;
  /** Last wave that is fully ready */
  lastReadyWave: number | null;
}
