import { z } from 'zod';

// Message types
export type MessageType = 'text' | 'coaching' | 'feedback' | 'announcement' | 'system';
export type MessageStatus = 'sent' | 'delivered' | 'read';

// Message schema
export const MessageSchema = z.object({
  id: z.string(),
  senderId: z.string(),
  recipientId: z.string(),
  content: z.string(),
  type: z.enum(['text', 'coaching', 'feedback', 'announcement', 'system']),
  status: z.enum(['sent', 'delivered', 'read']),
  createdAt: z.date(),
  readAt: z.date().optional(),
  attachments: z.array(z.object({
    id: z.string(),
    url: z.string(),
    name: z.string(),
    type: z.string(),
  })).optional(),
  metadata: z.record(z.string(), z.any()).optional(),
});

export type Message = z.infer<typeof MessageSchema>;

// Conversation schema
export const ConversationSchema = z.object({
  id: z.string(),
  participantIds: z.array(z.string()),
  lastMessage: MessageSchema.optional(),
  unreadCount: z.number(),
  createdAt: z.date(),
  updatedAt: z.date(),
  isArchived: z.boolean().default(false),
  isPinned: z.boolean().default(false),
});

export type Conversation = z.infer<typeof ConversationSchema>;

// Chat thread for group conversations
export const ChatThreadSchema = z.object({
  id: z.string(),
  conversationId: z.string(),
  name: z.string(),
  description: z.string().optional(),
  members: z.array(z.object({
    userId: z.string(),
    role: z.enum(['admin', 'member']),
    joinedAt: z.date(),
  })),
  createdAt: z.date(),
  createdBy: z.string(),
});

export type ChatThread = z.infer<typeof ChatThreadSchema>;

// Message input schema
export const SendMessageSchema = z.object({
  recipientId: z.string(),
  content: z.string().min(1).max(5000),
  type: z.enum(['text', 'coaching', 'feedback', 'announcement', 'system']).default('text'),
  attachments: z.array(z.object({
    url: z.string(),
    name: z.string(),
    type: z.string(),
  })).optional(),
  metadata: z.record(z.string(), z.any()).optional(),
});

export type SendMessageInput = z.infer<typeof SendMessageSchema>;

// Conversation list item
export const ConversationListItemSchema = z.object({
  id: z.string(),
  participantName: z.string(),
  participantAvatar: z.string().optional(),
  lastMessage: z.string(),
  lastMessageTime: z.date(),
  unreadCount: z.number(),
  isPinned: z.boolean(),
  isArchived: z.boolean(),
});

export type ConversationListItem = z.infer<typeof ConversationListItemSchema>;

// Message search result
export const MessageSearchResultSchema = z.object({
  id: z.string(),
  conversationId: z.string(),
  participantName: z.string(),
  content: z.string(),
  createdAt: z.date(),
  type: z.enum(['text', 'coaching', 'feedback', 'announcement', 'system']),
});

export type MessageSearchResult = z.infer<typeof MessageSearchResultSchema>;

// Typing indicator
export const TypingIndicatorSchema = z.object({
  userId: z.string(),
  conversationId: z.string(),
  isTyping: z.boolean(),
});

export type TypingIndicator = z.infer<typeof TypingIndicatorSchema>;

// Message reaction
export const MessageReactionSchema = z.object({
  id: z.string(),
  messageId: z.string(),
  userId: z.string(),
  emoji: z.string(),
  createdAt: z.date(),
});

export type MessageReaction = z.infer<typeof MessageReactionSchema>;
