import { create } from "zustand";
import { immer } from "zustand/middleware/immer";

interface ThreadStore {
    threads: ThreadsSchema;

    addThread: (thread: ThreadSchema) => void;
    setThreads: (threads: ThreadsSchema) => void;
    updateThread: (threadId: string, updates: Partial<ThreadSchema>) => void;
    removeThread: (threadId: string) => void;
    clearThreads: () => void;

    addReply: (threadId: string, reply: Reply) => void;
    updateReply: (threadId: string, replyId: string, updates: Partial<Reply>) => void;
    removeReply: (threadId: string, _id: string) => void;

    getThreadById: (threadId: string) => ThreadSchema | undefined;
}

export const createThreadsStore = () => {
    return create<ThreadStore>()(
        immer((set, get) => ({
            threads: [],

            addThread: (thread) => {
                set((state) => {
                    if (!state.threads.find((c) => c.threadId === thread.threadId)) {
                        state.threads.push(JSON.parse(JSON.stringify(thread)));
                    }
                });
            },

            setThreads: (threads) => {
                set((state) => {
                    state.threads = threads;
                });
            },

            updateThread: (threadId, updates) => {
                set((state) => {
                    const thread = state.threads.find((c) => c.threadId === threadId);
                    if (thread) {
                        Object.assign(thread, updates);
                    }
                });
            },

            removeThread: (threadId) => {
                set((state) => {
                    state.threads = state.threads.filter((c) => c.threadId !== threadId);
                });
            },

            clearThreads: () => {
                set((state) => {
                    state.threads = [];
                });
            },

            addReply: (threadId, thread) => {
                set((state) => {
                    const reply = state.threads.find((c) => c.threadId === threadId);
                    if (reply) {
                        reply.comments.push(thread);
                    }
                });
            },

            updateReply: (threadId, replyId, updates) => {
                set((state) => {
                    const reply = state.threads.find((c) => c.threadId === threadId);
                    if (reply) {
                        const thread = reply.comments.find((r) => r.replyId === replyId);
                        if (thread) {
                            Object.assign(thread, updates);
                        }
                    }
                });
            },

            removeReply: (threadId, _id) => {
                set((state) => {
                    const thread = state.threads.find((c) => c.threadId === threadId);
                    if (thread) {
                        thread.comments = thread.comments.filter((r) => r.replyId !== _id);
                    }
                });
            },

            getThreadById: (threadId) => {
                return get().threads.find((c) => c.threadId === threadId);
            },
        }))
    );
};

export type ThreadStoreType = ReturnType<typeof createThreadsStore>;
