import { ApplySpecSchema, type ApplySpec } from './types.js';

export interface ValidationResult {
  valid: boolean;
  data?: ApplySpec;
  blockers: string[];
  warnings: string[];
}

export function validate(raw: unknown): ValidationResult {
  const parsed = ApplySpecSchema.safeParse(raw);
  if (!parsed.success) {
    return {
      valid: false,
      blockers: parsed.error.issues.map((i) => `[${i.path.join('.')}] ${i.message}`),
      warnings: [],
    };
  }
  const data = parsed.data;
  const warnings: string[] = [];
  if (data.targetMigration && data.confirm === false) {
    warnings.push('a down-migration (targetMigration) is a 🟡 operation — it will need "confirm": true to proceed.');
  }
  return { valid: true, data, blockers: [], warnings };
}
