import { z } from 'zod';

const createConversationSchema = z.object({
  id: z.string().optional(),
  title: z.string().optional()
});
const updateConversationSchema = z.object({
  title: z.string().optional()
});
const chatRequestSchema = z.object({
  messages: z.array(
    z.union([
      // Format with content string
      z.object({
        role: z.enum(["system", "user", "assistant", "data"]),
        content: z.string(),
        id: z.string().optional()
      }),
      // Format with parts array (from UIMessage)
      z.object({
        role: z.enum(["system", "user", "assistant", "data"]),
        parts: z.array(
          z.object({
            type: z.string(),
            text: z.string().optional()
            // Allow other properties that might be present
          }).loose()
        ),
        id: z.string().optional(),
        metadata: z.any().optional()
      })
    ])
  ),
  conversationId: z.string().optional(),
  model: 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.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.array(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.string().optional()
});

export { chatRequestSchema, createConversationSchema, updateConversationSchema };
