import { AppShellSection, Box } from "@mantine/core";
import {
  IconMessageCircle,
  IconSettings,
  IconTicket,
} from "@tabler/icons-react";
import { useEffect, useState } from "react";
import { Outlet, useLocation } from "react-router";
import PageHeader from "@/layouts/shared/page.header";
import { PromoThemeShell } from "@/routes/promo-code-campaigns/_components/PromoThemeShell";
import { useRoleAccess } from "@/hooks/useRoleAccess";

/**
 * Subito AI Guru module layout: concierge chat, ticket history, admin tools.
 * Same themed-shell treatment as the kevo/promo-code pages.
 */
const GuruLayout = () => {
  const [activeIndex, setActiveIndex] = useState(0);
  const { pathname } = useLocation();
  const { checkClientAccess } = useRoleAccess();

  // checkClientAccess returns true for super admins too.
  const canAdmin = checkClientAccess("read", "guru-admin");

  const LINKS = [
    { Icon: IconMessageCircle, label: "Chat", href: "/admin/guru/chat" },
    { Icon: IconTicket, label: "My tickets", href: "/admin/guru/tickets" },
    ...(canAdmin
      ? [{ Icon: IconSettings, label: "Admin", href: "/admin/guru/admin" }]
      : []),
  ];

  useEffect(() => {
    const idx = LINKS.findIndex((item) => pathname.startsWith(item.href));
    setActiveIndex(idx === -1 ? 0 : idx);
  }, [pathname]);

  return (
    <AppShellSection>
      <PageHeader
        links={LINKS}
        activeIndex={activeIndex}
        setActiveIndex={setActiveIndex}
      />
      <PromoThemeShell>
        <Box py={20} px={28}>
          <Outlet />
        </Box>
      </PromoThemeShell>
    </AppShellSection>
  );
};

export default GuruLayout;
