import { create } from 'zustand';

export interface Participant {
  id: string;
  conversation_id: string;
  participant_id: string | null;
  participant_type: string;
  display_name: string | null;
  role: string;
}

export interface Conversation {
  id: string;
  title: string | null;
  is_locked: boolean;
  locked_by: string | null;
  is_direct: boolean;
  is_archived: boolean;
  is_pinned: boolean;
  created_by: string | null;
  created_by_type: string;
  last_msg_at: string | null;
  last_msg_preview: string | null;
  last_msg_sender_type: string | null;
  participants: Participant[];
  unread_count: number;
  metadata: Record<string, any>;
}

export interface MessageBlock {
  id: string;
  block_type: string;
  block_data: Record<string, any>;
  sort_order: number;
}

export interface MessageAttachment {
  id: string;
  file_id: string | null;
  file_source: string;
  file_name: string;
  file_type: string;
  file_size: number | null;
  thumbnail_path: string | null;
}

export interface Message {
  id: string;
  conversation_id: string;
  sender_id: string | null;
  sender_type: string;
  content: string;
  content_format: string;
  metadata: Record<string, any>;
  reply_to_id: string | null;
  is_pinned: boolean;
  created_at: string | null;
  edited_at: string | null;
  blocks: MessageBlock[];
  attachments: MessageAttachment[];
  reactions: any[];
}

export interface CommState {
  conversations: Conversation[];
  activeConversationId: string | null;
  messages: Record<string, Message[]>;
  typingUsers: Record<string, string[]>;
  unreadCounts: Record<string, number>;
  loading: boolean;

  setConversations: (convs: Conversation[]) => void;
  setActiveConversation: (id: string | null) => void;
  addMessage: (convId: string, msg: Message) => void;
  setMessages: (convId: string, msgs: Message[]) => void;
  updateConversation: (conv: Conversation) => void;
  setTyping: (convId: string, userIds: string[]) => void;
  setUnread: (convId: string, count: number) => void;
  setLoading: (loading: boolean) => void;
}

export const useCommStore = create<CommState>((set) => ({
  conversations: [],
  activeConversationId: null,
  messages: {},
  typingUsers: {},
  unreadCounts: {},
  loading: false,
  setConversations: (conversations) => set({ conversations }),
  setActiveConversation: (activeConversationId) => set({ activeConversationId }),
  addMessage: (convId, msg) => set((s) => ({
    messages: { ...s.messages, [convId]: [...(s.messages[convId] || []), msg] },
  })),
  setMessages: (convId, msgs) => set((s) => ({
    messages: { ...s.messages, [convId]: msgs },
  })),
  updateConversation: (conv) => set((s) => ({
    conversations: s.conversations.map((c) => (c.id === conv.id ? conv : c)),
  })),
  setTyping: (convId, userIds) => set((s) => ({
    typingUsers: { ...s.typingUsers, [convId]: userIds },
  })),
  setUnread: (convId, count) => set((s) => ({
    unreadCounts: { ...s.unreadCounts, [convId]: count },
  })),
  setLoading: (loading) => set({ loading }),
}));
