import z from "zod/v4";
import { EnvelopedMessage } from "../metadata.js";
import { applySetupPayloadSchema } from "./apply-setup.js";
import { setCatalogPayloadSchema } from "./set-catalog.js";
import { setSecretsPayloadSchema } from "./set-secrets.js";
import { setEnvVarsPayloadSchema } from "./set-env-vars.js";
import { setIdentityPayloadSchema } from "./set-identity.js";
import { initiateOAuthPayloadSchema } from "./initiate-oauth.js";
import { completeOAuthPayloadSchema } from "./complete-oauth.js";

// Raw payload schemas
export const McpxBoundPayloads = {
  applySetup: applySetupPayloadSchema,
  setCatalog: setCatalogPayloadSchema,
  setSecrets: setSecretsPayloadSchema,
  setEnvVars: setEnvVarsPayloadSchema,
  setIdentity: setIdentityPayloadSchema,
  initiateOAuth: initiateOAuthPayloadSchema,
  completeOAuth: completeOAuthPayloadSchema,
} as const;

export type McpxBoundPayload =
  (typeof McpxBoundPayloads)[keyof typeof McpxBoundPayloads];

export const MCPX_BOUND_EVENTS = {
  APPLY_SETUP: "apply-setup",
  SET_CATALOG: "set-catalog",
  SET_SECRETS: "set-secrets",
  SET_ENV_VARS: "set-env-vars",
  SET_IDENTITY: "set-identity",
  INITIATE_OAUTH: "initiate-oauth",
  COMPLETE_OAUTH: "complete-oauth",
} as const;

export type McpxBoundEventName =
  (typeof MCPX_BOUND_EVENTS)[keyof typeof MCPX_BOUND_EVENTS];

// This maps the kebab-case event names to their payload schemas (raw payloads)
export type McpxBoundPayloadOf<E extends McpxBoundEventName> =
  E extends typeof MCPX_BOUND_EVENTS.APPLY_SETUP
    ? z.input<typeof applySetupPayloadSchema>
    : E extends typeof MCPX_BOUND_EVENTS.SET_CATALOG
      ? z.input<typeof setCatalogPayloadSchema>
      : E extends typeof MCPX_BOUND_EVENTS.SET_SECRETS
        ? z.input<typeof setSecretsPayloadSchema>
        : E extends typeof MCPX_BOUND_EVENTS.SET_ENV_VARS
          ? z.input<typeof setEnvVarsPayloadSchema>
          : E extends typeof MCPX_BOUND_EVENTS.SET_IDENTITY
            ? z.input<typeof setIdentityPayloadSchema>
            : E extends typeof MCPX_BOUND_EVENTS.INITIATE_OAUTH
              ? z.input<typeof initiateOAuthPayloadSchema>
              : E extends typeof MCPX_BOUND_EVENTS.COMPLETE_OAUTH
                ? z.input<typeof completeOAuthPayloadSchema>
                : never;

// This maps the kebab-case event names to their enveloped message types
export type McpxBoundEnvelopedOf<E extends McpxBoundEventName> =
  EnvelopedMessage<McpxBoundPayloadOf<E>>;
