/**
 * Federation wire-format request schema.
 *
 * Source of truth: docs/federation/PRD.md §9 (query model).
 *
 * Pure — no NestJS, no DB, no Node-only APIs. Safe for browser/edge.
 */

import { z } from 'zod';
import { FEDERATION_VERBS } from './verbs.js';

// ---------------------------------------------------------------------------
// Query params — free-form key/value pairs passed alongside the request
// ---------------------------------------------------------------------------

const QueryParamsSchema = z.record(z.string(), z.string()).optional();

// ---------------------------------------------------------------------------
// Top-level request schema
// ---------------------------------------------------------------------------

export const FederationRequestSchema = z.object({
  /**
   * Verb being invoked. One of the M3 federation verbs.
   */
  verb: z.enum(FEDERATION_VERBS),

  /**
   * Resource path being queried, e.g. "tasks", "notes", "memory".
   * Forward-slash-separated for sub-resources (e.g. "teams/abc/tasks").
   */
  resource: z.string().min(1, { message: 'resource must not be empty' }),

  /**
   * Optional free-form query params (filters, sort, etc.).
   * Values are always strings; consumers parse as needed.
   */
  params: QueryParamsSchema,

  /**
   * Opaque pagination cursor returned by a previous list response.
   * Absent on first page.
   */
  cursor: z.string().optional(),
});

export type FederationRequest = z.infer<typeof FederationRequestSchema>;
