/**
 * commit CLI validate — Pure validation, no I/O
 */

import { CommitSpecSchema, type CommitSpec } from './types.js';

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

export function validate(spec: unknown): ValidationResult {
  const blockers: string[] = [];
  const warnings: string[] = [];

  let data: CommitSpec | undefined;
  try {
    data = CommitSpecSchema.parse(spec);
  } catch (err: unknown) {
    const e = err as any;
    if (e.errors) {
      for (const error of e.errors) {
        blockers.push(`${error.path?.join('.')}: ${error.message}`);
      }
    } else {
      blockers.push(`Spec parse failed: ${(err as Error).message}`);
    }
    return { valid: false, blockers, warnings };
  }

  if (data.message !== undefined && data.message.trim().length === 0) {
    blockers.push('message cannot be empty');
  }

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