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

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

export function validate(raw: unknown): ValidationResult {
  const parsed = SquashSpecSchema.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[] = [];
  if (!toPascalCase(data.description)) {
    blockers.push('[description] must contain at least one alphanumeric character');
  }
  if (data.aggressive) {
    warnings.push('aggressive=true — deletes everything not on the base branch. Use with caution.');
  }
  return { valid: blockers.length === 0, data, blockers, warnings };
}
