'use strict';

const z = require('zod');

const createConversationSchema = z.z.object({
  id: z.z.string().optional(),
  title: z.z.string().optional()
});
const updateConversationSchema = z.z.object({
  title: z.z.string().optional()
});
const chatRequestSchema = z.z.object({
  messages: z.z.array(
    z.z.union([
      // Format with content string
      z.z.object({
        role: z.z.enum(["system", "user", "assistant", "data"]),
        content: z.z.string(),
        id: z.z.string().optional()
      }),
      // Format with parts array (from UIMessage)
      z.z.object({
        role: z.z.enum(["system", "user", "assistant", "data"]),
        parts: z.z.array(
          z.z.object({
            type: z.z.string(),
            text: z.z.string().optional()
            // Allow other properties that might be present
          }).loose()
        ),
        id: z.z.string().optional(),
        metadata: z.z.any().optional()
      })
    ])
  ),
  conversationId: z.z.string().optional(),
  model: z.z.string().optional(),
  /**
   * Description of the current page context, injected into the AI system prompt.
   * Sent by ChatInterface when a page has registered context via useRegisterPageAIContext.
   */
  pageContext: z.z.string().max(16e3).optional(),
  /**
   * Names of client-side tools currently available on the page.
   * The server includes matching tool schemas in the streamText call.
   */
  availableTools: z.z.array(z.z.string()).optional(),
  /**
   * The routeName registered by the page via useRegisterPageAIContext.
   * Cross-validated server-side against each built-in tool's route allowlist
   * to prevent a page from claiming tools intended for a different route.
   */
  routeName: z.z.string().optional()
});

exports.chatRequestSchema = chatRequestSchema;
exports.createConversationSchema = createConversationSchema;
exports.updateConversationSchema = updateConversationSchema;
