/**
 * scaffold-dashboard-primitives/validate.ts — Args validation.
 */
import { existsSync, statSync } from 'node:fs';
import path from 'node:path';
import { ScaffoldDashboardPrimitivesInputSchema, type ScaffoldDashboardPrimitivesInput } from './types.js';

export interface ValidationResult {
  valid: boolean;
  spec: ScaffoldDashboardPrimitivesInput | null;
  errors: string[];
  warnings: string[];
}

export function validate(raw: unknown): ValidationResult {
  const parsed = ScaffoldDashboardPrimitivesInputSchema.safeParse(raw);
  if (!parsed.success) {
    return {
      valid: false,
      spec: null,
      errors: parsed.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`),
      warnings: [],
    };
  }
  const spec = parsed.data;
  const errors: string[] = [];
  const warnings: string[] = [];

  const abs = path.resolve(spec.projectPath);
  if (!existsSync(abs)) errors.push(`projectPath does not exist: ${abs}`);
  else if (!statSync(abs).isDirectory()) errors.push(`projectPath is not a directory: ${abs}`);
  else if (!existsSync(path.join(abs, 'src'))) {
    warnings.push(`projectPath has no "src" folder yet — it will be created.`);
  }

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