"use client";

import * as React from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { X } from "lucide-react";
import { Logo } from "@/components/brand/logo";
import { navForRole, isNavItemActive } from "@/lib/nav";
import { cn } from "@/lib/utils";
import type { Role } from "@prisma/client";

/**
 * Left rail navigation.
 *
 * Grouped under monospaced section rules; the active item is marked by an
 * accent bar in the margin rather than a filled pill.
 */
export function Sidebar({
  role,
  open,
  onClose,
}: {
  role: Role;
  open: boolean;
  onClose: () => void;
}) {
  const pathname = usePathname();
  const sections = React.useMemo(() => navForRole(role), [role]);

  return (
    <>
      {/* Mobile scrim */}
      {open ? (
        <div
          className="fixed inset-0 z-40 bg-brand-950/55 lg:hidden"
          onClick={onClose}
          aria-hidden="true"
        />
      ) : null}

      <aside
        className={cn(
          "fixed inset-y-0 left-0 z-50 flex w-64 flex-col border-r border-line bg-surface transition-transform duration-200 lg:translate-x-0 print:hidden",
          open ? "translate-x-0" : "-translate-x-full"
        )}
        aria-label="Navigasi utama"
      >
        <div className="flex h-16 shrink-0 items-center justify-between border-b border-line px-5">
          <Logo href="/dashboard" />
          <button
            type="button"
            onClick={onClose}
            className="p-1 text-tertiary transition-colors hover:bg-hover hover:text-primary lg:hidden"
            aria-label="Tutup navigasi"
          >
            <X className="size-4" />
          </button>
        </div>

        <nav className="flex-1 overflow-y-auto px-3 py-4">
          {sections.map((section) => (
            <div key={section.title} className="mb-6 last:mb-0">
              <p className="ws-label px-2.5 pb-2">{section.title}</p>
              <ul className="space-y-px">
                {section.items.map((item) => {
                  const active = isNavItemActive(pathname, item);
                  const Icon = item.icon;
                  return (
                    <li key={item.href}>
                      <Link
                        href={item.href}
                        onClick={onClose}
                        aria-current={active ? "page" : undefined}
                        className={cn(
                          "relative flex items-center gap-2.5 py-2 pl-3 pr-2.5 text-sm transition-colors",
                          active
                            ? "bg-sunken font-medium text-primary"
                            : "text-secondary hover:bg-hover hover:text-primary"
                        )}
                      >
                        {active ? (
                          <span
                            className="absolute inset-y-0 left-0 w-[3px] bg-accent-500"
                            aria-hidden="true"
                          />
                        ) : null}
                        <Icon
                          className={cn(
                            "size-4 shrink-0",
                            active ? "text-accent-600 dark:text-accent-400" : "text-tertiary"
                          )}
                        />
                        <span className="truncate">{item.label}</span>
                      </Link>
                    </li>
                  );
                })}
              </ul>
            </div>
          ))}
        </nav>
      </aside>
    </>
  );
}
