/**
 * scaffold-theme/validate.ts — Args validation (Zod + projectPath sanity check).
 */
import { existsSync, statSync } from 'node:fs';
import path from 'node:path';
import { ScaffoldThemeInputSchema, type ScaffoldThemeInput } from './types.js';

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

export function validate(raw: unknown): ValidationResult {
  const parsed = ScaffoldThemeInputSchema.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 };
}
