import { CreateSpecSchema, type CreateSpec } from './types.js';
import { toPascalCase } from '../lib/migration-name.js';

export interface ValidationResult {
  valid: boolean;
  data?: CreateSpec;
  blockers: string[];
  warnings: string[];
}

export function validate(raw: unknown): ValidationResult {
  const parsed = CreateSpecSchema.safeParse(raw);
  if (!parsed.success) {
    return {
      valid: false,
      blockers: parsed.error.issues.map((i) => `[${i.path.join('.')}] ${i.message}`),
      warnings: [],
    };
  }
  const data = parsed.data;
  const blockers: string[] = [];
  const warnings: string[] = [];

  const desc = toPascalCase(data.description);
  if (!desc) blockers.push('[description] must contain at least one alphanumeric character');
  if (desc && desc.length > 60) warnings.push('description is long (>60 chars after PascalCase) — keep it concise');

  return { valid: blockers.length === 0, data, blockers, warnings };
}
