/**
 * cli:cross-validate-stack — validate.ts
 * Validation structurelle (Zod) + verification que les repertoires existent
 * Ne fait PAS d'analyse de fichiers — c'est le role d'execute.ts
 */
import { existsSync, statSync } from 'node:fs'
import { CrossValidateInputSchema, type CrossValidateInput, type ValidationResult } from './types'

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

  // ─── Validation structurelle (Zod) ───
  const parsed = CrossValidateInputSchema.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

  // ─── Verification que le repertoire specs existe ───
  if (!existsSync(data.specs)) {
    blockers.push(`Specs directory not found: ${data.specs}`)
    return { valid: false, blockers, warnings }
  }

  const specsStat = statSync(data.specs)
  if (!specsStat.isDirectory()) {
    blockers.push(`Specs path is not a directory: ${data.specs}`)
    return { valid: false, blockers, warnings }
  }

  // ─── Verification que le repertoire src existe ───
  if (!existsSync(data.src)) {
    blockers.push(`Source directory not found: ${data.src}`)
    return { valid: false, blockers, warnings }
  }

  const srcStat = statSync(data.src)
  if (!srcStat.isDirectory()) {
    blockers.push(`Source path is not a directory: ${data.src}`)
    return { valid: false, blockers, warnings }
  }

  return {
    valid: true,
    data,
    blockers,
    warnings,
  }
}
