/**
 * validate.ts — Validation structurelle (Zod) + règles métier.
 * Ne connaît pas la DB, ne persiste rien.
 */
import { AbortSpecSchema, type AbortSpec } from './types.js';

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

export function validate(raw: unknown): ValidationResult {
  const blockers: string[] = [];
  const warnings: string[] = [];

  // ── Structural validation (Zod) ─────────────────────────
  const parsed = AbortSpecSchema.safeParse(raw);
  if (!parsed.success) {
    for (const issue of parsed.error.issues) {
      blockers.push(`[${issue.path.join('.')}] ${issue.message}`);
    }
    return { valid: false, blockers, warnings };
  }

  const data = parsed.data;

  // ── Business rules ──────────────────────────────────────
  // Minimal validation — mostly just structural

  return {
    valid: blockers.length === 0,
    data,
    blockers,
    warnings,
  };
}
