import { z } from "zod";

export const createConversationSchema = z.object({
  title: z.string().max(200).optional(),
  agentId: z.string().uuid().optional(),
});

export const updateConversationSchema = z.object({
  title: z.string().max(200).optional(),
  agentId: z.string().uuid().nullable().optional(),
  pinnedAt: z.string().datetime().nullable().optional(),
  archivedAt: z.string().datetime().nullable().optional(),
});

export const createMessageSchema = z.object({
  role: z.enum(["user", "assistant", "system"]),
  content: z.string().min(1).max(100_000),
  agentId: z.string().uuid().optional(),
  messageType: z.string().optional(),
});

export const handoffSchema = z.object({
  spec: z.object({
    what: z.string().min(1),
    why: z.string().min(1),
    constraints: z.string().optional().default(""),
    success: z.string().optional().default(""),
  }),
  targetRole: z.enum(["pm", "engineer", "general"]),
});

export type CreateConversation = z.infer<typeof createConversationSchema>;
export type UpdateConversation = z.infer<typeof updateConversationSchema>;
export type CreateMessage = z.infer<typeof createMessageSchema>;
export type Handoff = z.infer<typeof handoffSchema>;

export const searchMessagesSchema = z.object({
  q: z.string().min(2).max(200),
  limit: z.coerce.number().int().min(1).max(50).optional(),
});

export const branchConversationSchema = z.object({
  branchFromMessageId: z.string().uuid(),
});

export type SearchMessages = z.infer<typeof searchMessagesSchema>;
export type BranchConversation = z.infer<typeof branchConversationSchema>;

export const uploadChatFileSchema = z.object({
  conversationId: z.string().uuid().optional(),
  messageId: z.string().uuid().optional(),
  source: z.enum(["user_upload", "agent_generated"]).default("user_upload"),
  projectId: z.string().uuid().optional(),
});

export const createFileReferenceSchema = z.object({
  fileId: z.string().uuid(),
  messageId: z.string().uuid().optional(),
});

export type UploadChatFile = z.infer<typeof uploadChatFileSchema>;
export type CreateFileReference = z.infer<typeof createFileReferenceSchema>;
