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

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

  // ─── Validation structurelle (Zod) ───
  const parsed = ReadinessInputSchema.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}`)
  } else {
    const stat = statSync(data.specs)
    if (!stat.isDirectory()) {
      blockers.push(`Specs path is not a directory: ${data.specs}`)
    }
  }

  // ─── Verification que le repertoire src existe ───
  if (!existsSync(data.src)) {
    blockers.push(`Source directory not found: ${data.src}`)
  } else {
    const stat = statSync(data.src)
    if (!stat.isDirectory()) {
      blockers.push(`Source path is not a directory: ${data.src}`)
    }
  }

  if (blockers.length > 0) {
    return { valid: false, blockers, warnings }
  }

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