"use client";

import { useState, useEffect, useCallback } from "react";
import { useRouter } from "next/navigation";
import { MenuItem, MenuState } from "@/types/menu";
import { apiClient } from "@/lib/api/client";
import { AuthLogger } from "@/lib/authLogger";

/**
 * 메뉴 관련 비즈니스 로직을 관리하는 커스텀 훅
 * - API 호출은 apiClient를 통해 수행 (토큰 관리/401 처리 자동)
 * - 메뉴 로드 실패 시 토큰 삭제/리다이렉트하지 않음 (client.ts가 처리)
 */
export const useMenu = (user: any, authLoading: boolean) => {
  const router = useRouter();

  const [menuState, setMenuState] = useState<MenuState>({
    menu_list: [],
    expanded_menus: new Set(),
    isLoading: true,
  });

  /**
   * 메뉴 트리 구조 생성
   */
  const buildMenuTree = useCallback((menuItems: MenuItem[]): MenuItem[] => {
    const menuMap = new Map<string, MenuItem>();
    const rootMenus: MenuItem[] = [];

    menuItems.forEach((menu) => {
      const objId = String(menu.objid);
      const parentId = String(menu.parent_obj_id);
      menuMap.set(objId, { ...menu, objid: objId, parent_obj_id: parentId, children: [] });
    });

    menuItems.forEach((menu) => {
      const objId = String(menu.objid);
      const parentId = String(menu.parent_obj_id);
      const menuItem = menuMap.get(objId)!;

      if (parentId !== "-395553955") {
        const parent = menuMap.get(parentId);
        if (parent) {
          parent.children = parent.children || [];
          parent.children.push(menuItem);
        }
      } else {
        rootMenus.push(menuItem);
      }
    });

    return rootMenus.sort((a, b) => (a.seq || 0) - (b.seq || 0));
  }, []);

  /**
   * 메뉴 데이터 로드
   * - apiClient 사용으로 토큰/401 자동 처리
   * - 실패 시 빈 메뉴 유지 (로그인 리다이렉트는 client.ts 인터셉터가 담당)
   */
  const loadMenuData = useCallback(async () => {
    try {
      const response = await apiClient.get("/admin/user-menus");

      if (response.data?.success && response.data?.data) {
        setMenuState((prev: MenuState) => ({
          ...prev,
          menu_list: buildMenuTree(response.data.data || []),
          isLoading: false,
        }));
      } else {
        setMenuState((prev: MenuState) => ({ ...prev, isLoading: false }));
      }
    } catch (err: any) {
      AuthLogger.log("MENU_LOAD_FAIL", `메뉴 로드 실패: ${err?.response?.status || err?.message || "unknown"}`);
      setMenuState((prev: MenuState) => ({ ...prev, isLoading: false }));
    }
  }, [buildMenuTree]);

  /**
   * 메뉴 토글
   */
  const toggleMenu = useCallback((menuId: string) => {
    setMenuState((prev: MenuState) => {
      const newExpanded = new Set(prev.expanded_menus);
      if (newExpanded.has(menuId)) {
        newExpanded.delete(menuId);
      } else {
        newExpanded.add(menuId);
      }
      return {
        ...prev,
        expanded_menus: newExpanded,
      };
    });
  }, []);

  /**
   * 메뉴 클릭 처리
   */
  const handleMenuClick = useCallback(
    async (menu: MenuItem) => {
      if (menu.children && menu.children.length > 0) {
        toggleMenu(String(menu.objid));
      } else {
        const menuName = menu.menu_name_kor || menu.translated_name || "메뉴";
        if (typeof window !== "undefined") {
          localStorage.setItem("currentMenuName", menuName);
        }

        try {
          const menuObjid = menu.objid;
          if (menuObjid) {
            const { menuScreenApi } = await import("@/lib/api/screen");
            const assignedScreens = await menuScreenApi.getScreensByMenu(parseInt(menuObjid.toString()));

            if (assignedScreens.length > 0) {
              const firstScreen = assignedScreens[0];
              router.push(`/screens/${firstScreen.screen_id}?menuObjid=${menuObjid}`);
              return;
            }
          }
        } catch (error) {
          console.warn("할당된 화면 조회 실패:", error);
        }

        if (menu.menu_url) {
          router.push(menu.menu_url);
        } else {
          console.warn("메뉴에 URL이나 할당된 화면이 없습니다:", menu);
          const { toast } = await import("sonner");
          toast.warning("이 메뉴에는 연결된 페이지나 화면이 없습니다.");
        }
      }
    },
    [toggleMenu, router],
  );

  useEffect(() => {
    if (user && !authLoading) {
      loadMenuData();
    }
  }, [user, authLoading, loadMenuData]);

  return {
    menuList: menuState.menu_list,
    expandedMenus: menuState.expanded_menus,
    isMenuLoading: menuState.isLoading,
    handleMenuClick,
    toggleMenu,
    refreshMenus: loadMenuData,
  };
};
