import { z } from "zod";

{{#each handoffEntries}}
// ---------------------------------------------------------------------------
// {{id}}
// ---------------------------------------------------------------------------

export const {{schemaName}} = {{{indent (zodType schema) 0}}};

export type {{pascalName}} = z.infer<typeof {{schemaName}}>;

{{/each}}
// ---------------------------------------------------------------------------
// Schema registry
// ---------------------------------------------------------------------------

export const handoffSchemas = {
{{#each handoffEntries}}
  "{{id}}": {{schemaName}},
{{/each}}
} as const;

export type HandoffTypeId = keyof typeof handoffSchemas;

// ---------------------------------------------------------------------------
// Handoff envelope
// ---------------------------------------------------------------------------

export interface HandoffEnvelope<T extends HandoffTypeId = HandoffTypeId> {
  readonly type: T;
  readonly version: number;
  readonly payload: z.infer<(typeof handoffSchemas)[T]>;
}

// ---------------------------------------------------------------------------
// Factory functions — type-safe constructors with runtime validation
// ---------------------------------------------------------------------------

export const handoffs = {
{{#each handoffEntries}}
  {{varName}}(payload: {{pascalName}}): HandoffEnvelope<"{{id}}"> {
    return {
      type: "{{id}}" as const,
      version: 1,
      payload: {{schemaName}}.parse(payload),
    };
  },
{{/each}}
} as const;
