"use client";

import { useSession, signOut } from "next-auth/react";
import { usePathname } from "next/navigation";
import Link from "next/link";

export default function Nav() {
  const { data: session } = useSession();
  const pathname = usePathname();

  if (!session?.user) return null;

  const isAdmin = (session.user as any).role === "ADMIN";
  const links = [
    { href: "/settings", label: "Settings" },
    { href: "/settings/secrets", label: "Secrets" },
    { href: "/settings/credentials", label: "Credentials" },
    { href: "/settings/authorized-bots", label: "Bots" },
    { href: "/sessions", label: "Sessions" },
    ...(isAdmin
      ? [
          { href: "/admin/users", label: "Admin" },
          { href: "/admin/setup", label: "Setup" },
        ]
      : []),
  ];

  return (
    <nav className="nav">
      <Link href="/" style={{ fontWeight: 600, color: "var(--text)", marginRight: "auto" }}>
        Gemmbot
      </Link>
      {links.map((l) => (
        <Link key={l.href} href={l.href} className={pathname.startsWith(l.href) ? "active" : ""}>
          {l.label}
        </Link>
      ))}
      <button
        onClick={() => signOut({ callbackUrl: "/login" })}
        className="btn"
        style={{ padding: "0.25rem 0.5rem", fontSize: "0.75rem" }}
      >
        Sign out
      </button>
    </nav>
  );
}
