import React, {
  createContext,
  useContext,
  useState,
  useCallback,
  useMemo,
  ReactNode,
} from "react";
import {
  TableRegistration,
  TableOptionsContextValue,
} from "@/types/table-options";
import { useActiveTab } from "./ActiveTabContext";

const TableOptionsContext = createContext<TableOptionsContextValue | undefined>(
  undefined
);

export const TableOptionsProvider: React.FC<{ children: ReactNode }> = ({
  children,
}) => {
  const [registeredTables, setRegisteredTables] = useState<
    Map<string, TableRegistration>
  >(new Map());
  const [selectedTableId, setSelectedTableId] = useState<string | null>(null);

  /**
   * 테이블 등록
   */
  const registerTable = useCallback((registration: TableRegistration) => {
    setRegisteredTables((prev) => {
      const newMap = new Map(prev);
      newMap.set(registration.tableId, registration);

      // 첫 번째 테이블이면 자동 선택
      if (newMap.size === 1) {
        setSelectedTableId(registration.tableId);
      }

      return newMap;
    });
  }, []);

  /**
   * 테이블 등록 해제
   */
  const unregisterTable = useCallback(
    (tableId: string) => {
      setRegisteredTables((prev) => {
        const newMap = new Map(prev);
        const removed = newMap.delete(tableId);

        if (removed) {
          // 선택된 테이블이 제거되면 첫 번째 테이블 선택
          if (selectedTableId === tableId) {
            const firstTableId = newMap.keys().next().value;
            setSelectedTableId(firstTableId || null);
          }
        }

        return newMap;
      });
    },
    [selectedTableId]
  );

  /**
   * 특정 테이블 조회
   */
  const getTable = useCallback(
    (tableId: string) => {
      return registeredTables.get(tableId);
    },
    [registeredTables]
  );

  /**
   * 테이블 데이터 건수 업데이트
   */
  const updateTableDataCount = useCallback((tableId: string, count: number) => {
    setRegisteredTables((prev) => {
      const table = prev.get(tableId);
      if (table) {
        // 기존 테이블 정보에 dataCount만 업데이트
        const updatedTable = { ...table, dataCount: count };
        const newMap = new Map(prev);
        newMap.set(tableId, updatedTable);
        return newMap;
      }
      return prev;
    });
  }, []);

  // ActiveTab context 사용 (optional - 에러 방지)
  const activeTabContext = useActiveTab();

  /**
   * 현재 활성 탭의 테이블만 반환
   */
  const getActiveTabTables = useCallback(() => {
    const allTables = Array.from(registeredTables.values());
    const activeTabIds = activeTabContext.getAllActiveTabIds();
    
    // 활성 탭이 없으면 탭에 속하지 않은 테이블만 반환
    if (activeTabIds.length === 0) {
      return allTables.filter(table => !table.parentTabId);
    }
    
    // 활성 탭에 속한 테이블 + 탭에 속하지 않은 테이블
    return allTables.filter(table => 
      !table.parentTabId || activeTabIds.includes(table.parentTabId)
    );
  }, [registeredTables, activeTabContext]);

  /**
   * 특정 탭의 테이블만 반환
   */
  const getTablesForTab = useCallback((tabId: string) => {
    const allTables = Array.from(registeredTables.values());
    return allTables.filter(table => table.parentTabId === tabId);
  }, [registeredTables]);

  return (
    <TableOptionsContext.Provider
      value={{
        registeredTables,
        registerTable,
        unregisterTable,
        getTable,
        updateTableDataCount,
        selectedTableId,
        setSelectedTableId,
        getActiveTabTables,
        getTablesForTab,
      }}
    >
      {children}
    </TableOptionsContext.Provider>
  );
};

/**
 * Context Hook
 */
export const useTableOptions = () => {
  const context = useContext(TableOptionsContext);
  if (!context) {
    throw new Error("useTableOptions must be used within TableOptionsProvider");
  }
  return context;
};

