"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import {
  FileText,
  CalendarClock,
  Folder,
  LayoutDashboard,
  Link2,
  LogOut,
  MessageSquare,
  Sparkles,
  Star,
  Zap,
} from "lucide-react";
import { NAV_ITEMS } from "@/lib/constants";
import { cn } from "@/lib/utils";
import { useAuthStore } from "@/store/auth-store";
import {
  selectActiveProjectName,
  useProjectsHydrated,
} from "@/hooks/use-projects-hydrated";
import { useAccountIssues } from "@/hooks/use-connected-platforms";
import { useProjectsStore } from "@/store/projects-store";
import { ProjectSwitcher } from "./project-switcher";

const iconMap = {
  "layout-dashboard": LayoutDashboard,
  folder: Folder,
  sparkles: Sparkles,
  "message-square": MessageSquare,
  "calendar-clock": CalendarClock,
  zap: Zap,
  link: Link2,
  star: Star,
  "file-text": FileText,
} as const;

export function Sidebar() {
  const pathname = usePathname();
  const user = useAuthStore((s) => s.user);
  const logout = useAuthStore((s) => s.logout);
  const hydrated = useProjectsHydrated();
  const activeName = useProjectsStore(selectActiveProjectName);
  const footerLabel = !hydrated
    ? "No project selected"
    : activeName?.trim() || "No project selected";
  const accountIssues = useAccountIssues();

  return (
    <aside className="flex w-64 shrink-0 flex-col bg-brand-700 text-white">
      <div className="border-b border-white/10 px-5 py-5">
        <div className="flex items-center gap-2.5">
          <div className="flex h-8 w-8 items-center justify-center rounded-md bg-white/15 text-xs font-bold">
            PS
          </div>
          <span className="text-lg font-semibold tracking-tight">PostSync Pro</span>
        </div>
      </div>

      <ProjectSwitcher />

      <nav className="flex-1 space-y-0.5 overflow-y-auto px-3 pb-4">
        {NAV_ITEMS.map((item) => {
          const Icon = iconMap[item.icon];
          const isActive =
            pathname === item.href || pathname.startsWith(`${item.href}/`);

          return (
            <Link
              key={item.href}
              href={item.href}
              className={cn(
                "flex items-center gap-3 rounded-lg border-l-[3px] px-3 py-2.5 text-sm font-medium transition-colors",
                isActive
                  ? "border-l-white bg-white/20 text-white shadow-sm"
                  : "border-l-transparent text-white/75 hover:bg-white/10 hover:text-white",
              )}
            >
              <Icon className="h-4 w-4 shrink-0" strokeWidth={isActive ? 2.25 : 2} />
              {item.label}
            </Link>
          );
        })}
      </nav>

      <div className="space-y-2 border-t border-white/10 px-4 py-4">
        {accountIssues.length > 0 && (
          <Link
            href="/connected-accounts"
            className="flex items-center justify-between rounded-lg bg-red-500/20 px-3 py-2 text-xs font-medium text-red-100 hover:bg-red-500/30"
          >
            <span>
              {accountIssues.length} issue{accountIssues.length === 1 ? "" : "s"}
            </span>
            <span className="text-red-200/90">Reconnect</span>
          </Link>
        )}
        <div
          className="truncate rounded-lg bg-white/10 px-3 py-2 text-sm text-white/85"
          suppressHydrationWarning
        >
          {footerLabel}
        </div>
        <button
          type="button"
          onClick={() => void logout().then(() => (window.location.href = "/login"))}
          className="flex w-full items-center gap-2 rounded-lg px-3 py-2 text-sm text-white/70 transition-colors hover:bg-white/10 hover:text-white"
        >
          <LogOut className="h-4 w-4" />
          Sign out
        </button>
        {user && (
          <p className="truncate px-1 text-xs text-white/50">{user.name}</p>
        )}
      </div>
    </aside>
  );
}
