/**
 * cli:scaffold-tests — validate.ts
 */
import { ScaffoldTestsInputSchema, type ScaffoldTestsInput, type ValidationResult } from './types.js'
import { existsSync } from 'node:fs'

export function validateStructure(raw: unknown): ValidationResult {
  const result = ScaffoldTestsInputSchema.safeParse(raw)
  if (result.success) return { valid: true, errors: [], warnings: [] }
  const errors = result.error.issues.map(i => `[${i.path.join('.')}] ${i.message}`)
  return { valid: false, errors, warnings: [] }
}

export function validateBusinessRules(spec: ScaffoldTestsInput): ValidationResult {
  const errors: string[] = []
  const warnings: string[] = []

  if (!existsSync(spec.projectPath)) {
    errors.push(`Project path not found: ${spec.projectPath}`)
  }

  if (spec.layer === 'business' && spec.businessRules.length === 0) {
    warnings.push('No business rules provided — only validator tests will be generated')
  }

  return { valid: errors.length === 0, errors, warnings }
}

export function validate(raw: unknown): ValidationResult {
  const structural = validateStructure(raw)
  if (!structural.valid) return structural
  return validateBusinessRules(raw as ScaffoldTestsInput)
}
