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

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

export function validate(raw: unknown): ValidationResult {
  const parsed = RebaseSnapshotSpecSchema.safeParse(raw);
  if (!parsed.success) {
    return {
      valid: false,
      blockers: parsed.error.issues.map((i) => `[${i.path.join('.')}] ${i.message}`),
      warnings: [],
    };
  }
  const data = parsed.data;
  if (!toPascalCase(data.description)) {
    return { valid: false, blockers: ['[description] must contain at least one alphanumeric character'], warnings: [], data };
  }
  return { valid: true, data, blockers: [], warnings: [] };
}
