/**
 * validate.ts — Input validation for the reconcile-menu CLI.
 *
 * Stage 1: Zod structural validation.
 * Stage 2: Business validation (baRoot exists and is a directory).
 */
import { directoryExists } from '../../../../lib/fs.js';
import { ReconcileInputSchema } from './types.js';
import type { ValidationResult } from './types.js';

export async function validate(raw: unknown): Promise<ValidationResult> {
  const errors: string[] = [];
  const warnings: string[] = [];

  const parsed = ReconcileInputSchema.safeParse(raw);
  if (!parsed.success) {
    const zodErrors = parsed.error.issues.map(
      (i) => `${i.path.join('.')}: ${i.message}`,
    );
    return { valid: false, errors: zodErrors, warnings };
  }

  const spec = parsed.data;

  const baRootExists = await directoryExists(spec.baRoot);
  if (!baRootExists) {
    errors.push(`BA root directory not found: ${spec.baRoot}`);
    return { valid: false, spec, errors, warnings };
  }

  return { valid: true, spec, errors, warnings };
}
