
import React, { useEffect } from 'react';
import { useSocketStore } from '../../../store/builder/store';
import { getUserData } from '../../../functions/getUserData';
import { getAllProjects } from '../../../services/dashboard/getAllProjects';
import { recentlyViewed } from '../../../services/dashboard/recentlyViewed';

interface Project {
    _id: string;
    projectName: string;
    thumbnail: string;
    createdBy: string;
    projectUuid?: string;
    createdAt: string;
    isViewed?: string
}
interface RecentProject {
    _id: string;
    projectName: string;
    thumbnail: string;
    createdBy: { _id: string, userName: string };
    projectUuid?: string;
    createdAt: string;
    isViewed?: string
}

interface RecentProjectData {
    [key: string]: RecentProject[];
}
interface ProjectsData {
    [key: string]: Project[];
}
interface ProjectSocketResProps {
    setRecentProjects?: React.Dispatch<React.SetStateAction<RecentProjectData>>;
    setWorkspaceProjects?: React.Dispatch<React.SetStateAction<ProjectsData>>;
    setIsSearchActive?: React.Dispatch<React.SetStateAction<boolean>>;
}

const ProjectSocketRes = ({
    setRecentProjects,
    setWorkspaceProjects,
    setIsSearchActive,
}: ProjectSocketResProps) => {
    const { projectSocket } = useSocketStore();
    const { userId, organization } = getUserData();

    useEffect(() => {
        if (!projectSocket) return;

        const handleAdd = (data: any) => {
            // console.log('Add:', data);
        };

        const handleDelete = (data: any) => {
            // console.log('Delete:', data);
        };

        const handleUpdate = (data: any) => {
            // console.log('Update:', data);
        };

        const handleTrashDelete = (data: any) => {
            // console.log('Trash Delete:', data);
        };

        const handleDuplicate = async (data: any) => {
            console.log("Project duplicate response:", data);
            if (data?.message === "Project Duplicated successfully") {
                if (setWorkspaceProjects) {
                    const allProjects = await getAllProjects(userId, organization);
                    // console.log('allProjects: ', allProjects);
                    setWorkspaceProjects(allProjects);
                } else if (setRecentProjects) {
                    const recentProjects = await recentlyViewed(organization, userId);
                    setRecentProjects && setRecentProjects(recentProjects);
                }
                setIsSearchActive && setIsSearchActive(false);
            } else {
                console.warn("Duplication failed or unexpected response.");
            }
        };

        projectSocket.on("v1-project:response:add", handleAdd);
        projectSocket.on("v1-project:response:delete", handleDelete);
        projectSocket.on("v1-project:response:update", handleUpdate);
        projectSocket.on("v1-project:response:Duplicate", handleDuplicate);
        projectSocket.on("v1:trash:response:delete", handleTrashDelete);

        return () => {
            projectSocket.off("v1-project:response:add", handleAdd);
            projectSocket.off("v1-project:response:delete", handleDelete);
            projectSocket.off("v1-project:response:update", handleUpdate);
            projectSocket.off("v1-project:response:Duplicate", handleDuplicate);
            projectSocket.off("v1:trash:response:delete", handleTrashDelete);
        };
    }, [projectSocket, userId, organization]);

    return null;
};

export default ProjectSocketRes;
