import React, { useEffect, useMemo, useState } from "react";
import Breadcrumb from "../../components/common/Breadcrumb";
import IconButton from "../../components/common/IconButton";
import GlobalModel from "../../components/common/GlobalModel";
import ManageDirectory from "../../components/Directory/ManageDirectory";
import ListViewDirectory from "../../components/Directory/ListViewDirectory";
import { useBuckets, useDirectory } from "../../hooks/useDirectory";
import { DirectoryRepository } from "../../repositories/DirectoryRepository";
import { cacheData, getCachedData } from "../../slices/apiDataManager";
import showToast from "../../services/toastService";
import UpdateContact from "../../components/Directory/UpdateContact";
import CardViewDirectory from "../../components/Directory/CardViewDirectory";
import { useContactCategory } from "../../hooks/masterHook/useMaster";
import usePagination from "../../hooks/usePagination";
import { ITEMS_PER_PAGE } from "../../utils/constants";
import ProfileContactDirectory from "../../components/Directory/ProfileContactDirectory";
import ConfirmModal from "../../components/common/ConfirmModal";
import DirectoryListTableHeader from "./DirectoryListTableHeader";
import DirectoryPageHeader from "./DirectoryPageHeader";
import ManageBucket from "../../components/Directory/ManageBucket";
import { useFab } from "../../Context/FabContext";
import { DireProvider, useDir } from "../../Context/DireContext";
import NotesCardViewDirectory from "../../components/Directory/NotesCardViewDirectory";
const Directory = ({ IsPage = true, prefernceContacts }) => {
  const [projectPrefernce, setPerfence] = useState(null);
  const [IsActive, setIsActive] = useState(true);
  const [isOpenModal, setIsOpenModal] = useState(false);
  const [isOpenModalNote, setIsOpenModalNote] = useState(false);
  const [selectedContact, setSelectedContact] = useState(null);
  const [open_contact, setOpen_contact] = useState(null);
  const [ContactList, setContactList] = useState([]);
  const [contactCategories, setContactCategories] = useState([]);
  const [searchText, setSearchText] = useState("");
  const [viewType, setViewType] = useState("notes");
  const [selectedBucketIds, setSelectedBucketIds] = useState([]);
  const [deleteContact, setDeleteContact] = useState(null);
  const [IsDeleting, setDeleting] = useState(false);
  const [openBucketModal, setOpenBucketModal] = useState(false);
  const [notes, setNotes] = useState([]);
    const [filterAppliedNotes, setFilterAppliedNotes] = useState([]);
  // const [selectedOrgs, setSelectedOrgs] = useState([]);
  // ✅ Changed to an array for multiple selections
  const [selectedNoteNames, setSelectedNoteNames] = useState([]);
  const [tempSelectedBucketIds, setTempSelectedBucketIds] = useState([]);
  const [tempSelectedCategoryIds, setTempSelectedCategoryIds] = useState([]);
  const { setActions } = useFab();
  const { dirActions, setDirActions } = useDir();
  const { contacts, loading, refetch } = useDirectory(
    IsActive,
    projectPrefernce
  );
  const { contactCategory, loading: contactCategoryLoading } =
    useContactCategory();
  const { buckets, refetch: refetchBucket } = useBuckets();
  const submitContact = async (data) => {
    try {
      let response;
      let updatedContacts;
      const contacts_cache = getCachedData("contacts")?.data || [];
      if (selectedContact) {
        response = await DirectoryRepository.UpdateContact(data.id, data);
        updatedContacts = contacts_cache.map((contact) =>
          contact.id === data.id ? response.data : contact
        );
        showToast("Contact updated successfully", "success");
        setIsOpenModal(false);
        setSelectedContact(null);
      } else {
        response = await DirectoryRepository.CreateContact(data);
        updatedContacts = [...contacts_cache, response.data];
        showToast("Contact created successfully", "success");
        setIsOpenModal(false);
      }
      refetch(IsActive, prefernceContacts);
      refetchBucket();
    } catch (error) {
      const msg =
        error.response?.data?.message ||
        error.message ||
        "Error occurred during API call!";
      showToast(msg, "error");
    }
  };
  const handleDeleteContact = async (overrideId = null) => {
    try {
      if (!IsActive) {
        setDirActions((prev) => ({ ...prev, action: true }));
      } else {
        setDeleting(true);
      }
      const id = overrideId || (!IsActive ? dirActions.id : deleteContact);
      if (!id) {
        showToast("No contact selected for deletion", "error");
        return;
      }
      await DirectoryRepository.DeleteContact(id, !IsActive);
      const updatedContacts = ContactList.filter((c) => c.id !== id);
      setContactList(updatedContacts);
      cacheData("Contacts", { data: updatedContacts, isActive: IsActive });
      showToast(
        `Contact ${IsActive ? "Deleted" : "Restored"} successfully`,
        "success"
      );
      setDeleteContact(null);
      refetchBucket();
      setDirActions({ action: false, id: null });
      setDeleting(false);
    } catch (error) {
      const msg =
        error?.response?.data?.message ||
        error.message ||
        "Error occurred during API call";
      showToast(msg, "error");
      setDeleting(false);
      setDirActions({ action: false, id: null });
    }
  };
  const closedModel = () => {
    setIsOpenModal(false);
    setSelectedContact(null);
    setOpen_contact(null);
  };
  const [selectedCategoryIds, setSelectedCategoryIds] = useState(
    contactCategory.map((category) => category.id)
  );
  useEffect(() => {
    setContactList(contacts);
    setTempSelectedCategoryIds([]);
    setTempSelectedBucketIds([]);
  }, [contacts]);
  const usedCategoryIds = [
    ...new Set(contacts.map((c) => c.contactCategory?.id)),
  ];
  const filteredCategories = contactCategory.filter((category) =>
    usedCategoryIds.includes(category.id)
  );
  const handleTempBucketChange = (id) => {
    setTempSelectedBucketIds((prev) =>
      prev.includes(id) ? prev.filter((bid) => bid !== id) : [...prev, id]
    );
  };
  const handleTempCategoryChange = (id) => {
    setTempSelectedCategoryIds((prev) =>
      prev.includes(id) ? prev.filter((cid) => cid !== id) : [...prev, id]
    );
  };
  const usedBucketIds = [
    ...new Set(contacts.flatMap((c) => c.bucketIds || [])),
  ];
  const filteredBuckets = buckets.filter((bucket) =>
    usedBucketIds.includes(bucket.id)
  );
  const filteredContacts = useMemo(() => {
    return ContactList.filter((c) => {
      const matchesSearch =
        c.name.toLowerCase().includes(searchText.toLowerCase()) ||
        c.organization.toLowerCase().includes(searchText.toLowerCase());
      const matchesCategory =
        selectedCategoryIds.length === 0 ||
        selectedCategoryIds.includes(c.contactCategory?.id);
      const matchesBucket =
        selectedBucketIds.length === 0 ||
        (c.bucketIds || []).some((id) => selectedBucketIds.includes(id));
      return matchesSearch && matchesCategory && matchesBucket;
    }).sort((a, b) => a?.name?.localeCompare(b.name));
  }, [
    ContactList,
    searchText,
    selectedCategoryIds,
    selectedBucketIds,
    selectedContact,
  ]);
  const applyFilter = () => {
    setSelectedBucketIds(tempSelectedBucketIds);
    setSelectedCategoryIds(tempSelectedCategoryIds);
  };
  const clearFilter = () => {
    setTempSelectedBucketIds([]);
    setTempSelectedCategoryIds([]);
    setSelectedBucketIds([]);
    setSelectedCategoryIds([]);
  };
  const { currentPage, totalPages, currentItems, paginate } = usePagination(
    filteredContacts,
    ITEMS_PER_PAGE
  );
  const renderModalContent = () => {
    if (selectedContact) {
      return (
        
No contact found
)} {!loading && (viewType === "card" || viewType === "list") && contacts?.length > 0 && currentItems.length === 0 && (No matching contact found
)}