/**
 * smoke-http/validate.ts — Args validation (Zod only — no I/O).
 */
import { SmokeHttpArgsSchema, type SmokeHttpArgs } from './types.js';

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

export function validateArgs(raw: unknown): ArgsValidationResult {
  const parsed = SmokeHttpArgsSchema.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[] = [];

  // Probes with duplicate URLs are probably a user mistake — warn (but not block).
  const seen = new Set<string>();
  for (const p of args.probes) {
    const key = `${p.method} ${p.url}`;
    if (seen.has(key)) {
      errors.push(`duplicate probe: ${key} — each URL+method combo should appear once.`);
    }
    seen.add(key);
  }

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