import { z } from "zod";

const ChatMessageSchema = z.object({
  id: z.string(),
  role: z.string(),
  userId: z.string().optional(),
  chatId: z.string().optional(),
  createdDate: z.string(),
  isRead: z.boolean(),
  type: z.enum(["text", "image", "voice"]),
  text: z.string().optional(),
  isLast: z.boolean().optional(),
  suggestions: z.array(z.string()).optional(),
  sentWithZeroBalance: z.boolean().optional(),
});

const ChatSchema = z.object({
  id: z.string(),
  assistantId: z.string(),
  assistantName: z.string(),
  assistantAvatar: z.string(),
  lastMessage: ChatMessageSchema.nullable(),
  unreadCount: z.number(),
  updatedAt: z.string(),
  status: z.string(),
  assistantStatus: z.string(),
  category: z.string(),
});

const CategorizedChatsSchema = z.record(z.array(ChatSchema));

const CreateAllChatsResponseSchema = z.object({
  success: z.boolean(),
  chatIds: z.array(z.string()),
  count: z.number(),
});

const CreateChatResponseSchema = z.object({
  chatId: z.string(),
});

const GetChatsListResponseSchema = z.object({
  categorizedChats: CategorizedChatsSchema,
  startedChats: z.array(ChatSchema),
  unreadChats: z.array(ChatSchema),
  totalUnreadCount: z.number(),
});

const GetChatMessagesResponseSchema = z.object({
  messages: z.array(ChatMessageSchema),
  totalCount: z.number(),
  page: z.number(),
  limit: z.number(),
});

// Chat settings
const ChatSettingsSchema = z.object({
  autoTopUp: z.boolean(),
  topUpAmount: z.number(),
});

const GetMyChatSettingsResponseSchema = z.object({
  success: z.boolean(),
  settings: ChatSettingsSchema,
});

const UpdateMyChatSettingsResponseSchema = z.object({
  success: z.boolean(),
  settings: ChatSettingsSchema,
});

export type IChat = z.infer<typeof ChatSchema>;
export type ICategorizedChats = z.infer<typeof CategorizedChatsSchema>;
export type ICreateAllChatsResponse = z.infer<
  typeof CreateAllChatsResponseSchema
>;
export type ICreateChatResponse = z.infer<typeof CreateChatResponseSchema>;
export type IGetChatsListResponse = z.infer<typeof GetChatsListResponseSchema>;
export type IChatMessage = z.infer<typeof ChatMessageSchema>;
export type IGetChatMessagesResponse = z.infer<
  typeof GetChatMessagesResponseSchema
>;
export type IChatSettings = z.infer<typeof ChatSettingsSchema>;
export type IGetMyChatSettingsResponse = z.infer<
  typeof GetMyChatSettingsResponseSchema
>;
export type IUpdateMyChatSettingsResponse = z.infer<
  typeof UpdateMyChatSettingsResponseSchema
>;

export {
  ChatMessageSchema,
  ChatSettingsSchema,
  CreateAllChatsResponseSchema,
  CreateChatResponseSchema,
  GetChatMessagesResponseSchema,
  GetChatsListResponseSchema,
  GetMyChatSettingsResponseSchema,
  UpdateMyChatSettingsResponseSchema,
};
