/**
 * cli:scaffold-doc — validate.ts
 * Zod validation (returns the parsed value WITH defaults applied) + non-blocking
 * sanity checks. Never blocks on app-shape differences — generate degrades to
 * warnings + wiring instructions.
 */
import { existsSync } from 'node:fs'
import { ScaffoldDocInputSchema, type ValidationResult } from './types.js'

export function validate(raw: unknown): ValidationResult {
  const parsed = ScaffoldDocInputSchema.safeParse(raw)
  if (!parsed.success) {
    return {
      valid: false,
      errors: parsed.error.issues.map((i) => `[${i.path.join('.')}] ${i.message}`),
      warnings: [],
    }
  }
  const d = parsed.data
  if (!existsSync(d.projectPath)) {
    return { valid: false, errors: [`projectPath does not exist: ${d.projectPath}`], warnings: [] }
  }
  const warnings: string[] = []
  if (!d.i18n) {
    warnings.push('No i18n payload — author the 4 locale JSON files separately (templates.md).')
  } else if (!d.i18n.fr) {
    warnings.push('i18n payload has no `fr` (source language) — the FR file will not be written.')
  }
  if (d.type === 'user' && !d.application) {
    warnings.push('user-type doc without `application` — the UserIndexPage wiring instruction will be generic.')
  }
  return { valid: true, errors: [], warnings, value: d }
}
