/**
 * cli:test-report — validate.ts
 */
import { TestReportInputSchema, type TestReportInput, type ValidationResult } from './types.js'
import { existsSync } from 'node:fs'

export function validateStructure(raw: unknown): ValidationResult {
  const result = TestReportInputSchema.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: TestReportInput): ValidationResult {
  const errors: string[] = []
  if (!existsSync(spec.projectPath)) {
    errors.push(`Project path not found: ${spec.projectPath}`)
  }
  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 TestReportInput)
}
