/**
 * project-inventory/validate.ts — Args validation (Zod + projectPath sanity).
 */
import { existsSync, statSync } from 'node:fs';
import path from 'node:path';
import { ProjectInventoryArgsSchema, type ProjectInventoryArgs } from './types.js';

export interface ArgsValidationResult {
  valid: boolean;
  args: ProjectInventoryArgs | null;
  errors: string[];
}

export function validateArgs(raw: unknown): ArgsValidationResult {
  const parsed = ProjectInventoryArgsSchema.safeParse(raw);
  if (!parsed.success) {
    return {
      valid: false,
      args: null,
      errors: parsed.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`),
    };
  }

  const args = parsed.data;
  const errors: string[] = [];

  const abs = path.resolve(args.projectPath);
  if (!existsSync(abs)) {
    errors.push(`projectPath does not exist: ${abs}`);
  } else if (!statSync(abs).isDirectory()) {
    errors.push(`projectPath is not a directory: ${abs}`);
  }

  return { valid: errors.length === 0, args, errors };
}
