import { Bell, BookOpenCheck, HelpCircle, LogOut, ScrollText } from "lucide-react";
import { Link, useLocation, useNavigate } from "react-router-dom";

import { useAuth } from "@ump/shared";
import {
  Sidebar,
  SidebarContent,
  SidebarFooter,
  SidebarGroup,
  SidebarGroupContent,
  SidebarHeader,
  SidebarMenu,
  SidebarMenuButton,
  SidebarMenuItem,
  SidebarSeparator,
  useSidebar,
} from "@/components/ui/sidebar";
import {
  ApplicantSidebarMenuItem,
  type MenuItemDescriptor,
} from "@/components/applicant/ApplicantSidebarMenuItem";

const LOGO_SRC = "/logo.png";

// Publisher journey entry — publish papers from research results.
const papersItem: MenuItemDescriptor = {
  title: "Công bố bài báo",
  icon: BookOpenCheck,
  url: "/dashboard/papers",
};

// Governance — the operating philosophy that governs the publish workflow.
const governanceItem: MenuItemDescriptor = {
  title: "Triết lý vận hành",
  icon: ScrollText,
  url: "/dashboard/governance",
};

const systemItems: MenuItemDescriptor[] = [
  { title: "Thông báo", icon: Bell, url: "/dashboard/notifications" },
  { title: "Trợ giúp", icon: HelpCircle, url: "/dashboard/help" },
];

export function ApplicantDashboardSidebar() {
  const location = useLocation();
  const navigate = useNavigate();
  const { state } = useSidebar();
  const { logout } = useAuth();
  const isCollapsed = state === "collapsed";

  const handleLogout = () => {
    logout();
    navigate("/login");
  };

  // Papers item is active for any /dashboard/papers* route.
  const pathname = location.pathname.startsWith("/dashboard/papers")
    ? papersItem.url
    : location.pathname;

  return (
    <Sidebar collapsible="icon">
      <SidebarHeader className="h-16 justify-center border-b border-sidebar-border px-4">
        <Link to="/dashboard" className="flex items-center gap-2">
          <img
            src={LOGO_SRC}
            alt="Đại học Y Dược Thành phố Hồ Chí Minh"
            className="h-9 w-9 object-contain flex-shrink-0"
          />
          {!isCollapsed && (
            <div className="leading-tight">
              <div className="font-serif text-sm font-semibold text-sidebar-foreground">
                Đại học Y Dược
              </div>
              <div className="text-[11px] text-muted-foreground">Thành phố Hồ Chí Minh</div>
            </div>
          )}
        </Link>
      </SidebarHeader>

      <SidebarContent>
        <SidebarGroup>
          <SidebarGroupContent>
            <SidebarMenu>
              <ApplicantSidebarMenuItem item={papersItem} pathname={pathname} />
              <ApplicantSidebarMenuItem item={governanceItem} pathname={location.pathname} />
            </SidebarMenu>
          </SidebarGroupContent>
        </SidebarGroup>

        <SidebarSeparator />

        <SidebarGroup>
          <SidebarGroupContent>
            <SidebarMenu>
              {systemItems.map((item) => (
                <ApplicantSidebarMenuItem key={item.url} item={item} pathname={location.pathname} />
              ))}
            </SidebarMenu>
          </SidebarGroupContent>
        </SidebarGroup>
      </SidebarContent>

      <SidebarFooter className="border-t border-sidebar-border p-2">
        <SidebarMenu>
          <SidebarMenuItem>
            <SidebarMenuButton
              onClick={handleLogout}
              tooltip="Đăng xuất"
              className="text-destructive text-bg-black hover:text-destructive cursor-pointer"
            >
              <LogOut className="h-4 w-4" />
              <span>Đăng xuất</span>
            </SidebarMenuButton>
          </SidebarMenuItem>
        </SidebarMenu>
      </SidebarFooter>
    </Sidebar>
  );
}
