export interface Contact {
  id: string
  name: string
  email?: string
  phone?: string
  role: 'parent' | 'teacher' | 'staff' | 'student'
  student_name?: string
  class_name?: string
  notes?: string
  tags: string[]
  avatar_url?: string
  preferred_channel: 'email' | 'matrix' | 'pwa'
  online: boolean
  last_seen?: string
  created_at: string
  updated_at: string
}

export interface Message {
  id: string
  conversation_id: string
  sender_id: string // "self" for own messages
  content: string
  content_type: 'text' | 'file' | 'image' | 'voice'
  file_url?: string
  file_name?: string
  timestamp: string
  read: boolean
  read_at?: string
  delivered: boolean
  send_email: boolean
  email_sent: boolean
  email_sent_at?: string
  email_error?: string
  reply_to?: string // ID of message being replied to
  reactions?: { emoji: string; user_id: string }[]
}

export interface Conversation {
  id: string
  participant_ids: string[]
  group_id?: string
  created_at: string
  updated_at: string
  last_message?: string
  last_message_time?: string
  unread_count: number
  is_group: boolean
  title?: string
  typing?: boolean // Someone is typing
  pinned?: boolean
  muted?: boolean
  archived?: boolean
}

export interface MessageTemplate {
  id: string
  name: string
  content: string
  created_at: string
}

export interface MessagesStats {
  total_contacts: number
  total_conversations: number
  total_messages: number
  unread_messages: number
}

export interface MessagesContextType {
  // Data
  contacts: Contact[]
  conversations: Conversation[]
  messages: Record<string, Message[]> // conversationId -> messages
  templates: MessageTemplate[]
  stats: MessagesStats

  // Computed
  unreadCount: number
  recentConversations: Conversation[]

  // Actions
  fetchContacts: () => Promise<void>
  fetchConversations: () => Promise<void>
  fetchMessages: (conversationId: string) => Promise<Message[]>
  sendMessage: (conversationId: string, content: string, sendEmail?: boolean, replyTo?: string) => Promise<Message | null>
  markAsRead: (conversationId: string) => Promise<void>
  createConversation: (contactId: string) => Promise<Conversation | null>
  addReaction: (messageId: string, emoji: string) => void
  deleteMessage: (conversationId: string, messageId: string) => void
  pinConversation: (conversationId: string) => void
  muteConversation: (conversationId: string) => void

  // State
  isLoading: boolean
  error: string | null
  currentConversationId: string | null
  setCurrentConversationId: (id: string | null) => void
}
