import { AppShellSection, Box } from "@mantine/core";
import {
  IconBook2,
  IconCloudUpload,
  IconFlag,
  IconMessagePlus,
} 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";

/**
 * Kevo (Live Help agent) module layout: flags review, knowledge corrections,
 * publishing. Same themed-shell treatment as the promo-code pages.
 */
const KevoLayout = () => {
  const [activeIndex, setActiveIndex] = useState(0);
  const { pathname } = useLocation();
  const { checkClientAccess } = useRoleAccess();

  const canCreate = checkClientAccess("create", "kevo");
  const canSeePublish = checkClientAccess("read", "kevo");

  const LINKS = [
    { Icon: IconFlag, label: "Flags", href: "/admin/kevo/flags" },
    { Icon: IconBook2, label: "Knowledge", href: "/admin/kevo/knowledge" },
    ...(canSeePublish
      ? [
          {
            Icon: IconCloudUpload,
            label: "Publish",
            href: "/admin/kevo/publish",
          },
        ]
      : []),
    ...(canCreate
      ? [
          {
            Icon: IconMessagePlus,
            label: "Manual flag",
            href: "/admin/kevo/manual",
          },
        ]
      : []),
  ];

  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 KevoLayout;
