/**
 * Zod schemas for inbound webhook payloads.
 * `require-zod-validation-in-routes` requires `await c.req.json()` to be parsed
 * by one of these schemas.
 *
 * Both schemas use `.passthrough()` on nested objects so unmodelled update types
 * (e.g. Slack slash commands, Telegram edited_message, callback_query) continue
 * to flow through and return 200 instead of a 400. Only the fields actually read
 * by the routes are typed explicitly.
 */
import { z } from 'zod'

// ── Slack ─────────────────────────────────────────────────────────────────────

export const slackWebhookSchema = z
  .object({
    type: z.string().optional(),
    challenge: z.unknown().optional(),
    team_id: z.string().optional(),
    team: z
      .object({
        id: z.string().optional(),
      })
      .passthrough()
      .optional(),
    event: z
      .object({
        type: z.string().optional(),
        text: z.string().optional(),
        user: z.string().optional(),
        channel: z.string().optional(),
        ts: z.unknown().optional(),
      })
      .passthrough()
      .optional(),
  })
  .passthrough()

export type SlackWebhookBody = z.infer<typeof slackWebhookSchema>

// ── Telegram ──────────────────────────────────────────────────────────────────

export const telegramUpdateSchema = z
  .object({
    update_id: z.number(),
    message: z
      .object({
        message_id: z.number(),
        from: z
          .object({
            id: z.number(),
            username: z.string().optional(),
            first_name: z.string().optional(),
          })
          .passthrough()
          .optional(),
        chat: z
          .object({
            id: z.number(),
            type: z.string(),
          })
          .passthrough(),
        text: z.string().optional(),
        caption: z.string().optional(),
        document: z
          .object({
            file_id: z.string(),
            file_name: z.string().optional(),
          })
          .passthrough()
          .optional(),
      })
      .passthrough()
      .optional(),
  })
  .passthrough()

export type TelegramUpdateBody = z.infer<typeof telegramUpdateSchema>
