"use client";

import Link from "next/link";
import { CircleHelp, LogOut } from "lucide-react";

import { LogoutButton } from "@/components/auth/logout-button";
import { LanguageSwitcher } from "@/components/layout/language-switcher";
import { TopbarSearch } from "@/components/layout/topbar-search";
import { useLocale } from "@/components/providers/locale-provider";
import type { SessionUser } from "@/lib/auth";

type TopbarProps = {
  title: string;
  description: string;
  user: SessionUser;
};

export function Topbar({ title, description, user }: TopbarProps) {
  const { dict } = useLocale();

  return (
    <>
      <header className="sticky top-0 z-40 flex h-14 items-center gap-4 border-b border-line/70 bg-white px-6 shadow-panel">
        <div className="hidden min-w-0 flex-1 lg:block">
          <TopbarSearch placeholder={dict.shell.searchPlaceholder} />
        </div>
        <div className="ml-auto flex items-center gap-4">
          <LanguageSwitcher />
          <Link
            href="/help"
            className="rounded-lg p-2 text-slate-600 transition hover:bg-slate-50"
            aria-label={dict.common.help}
          >
            <CircleHelp className="h-5 w-5" />
          </Link>
          <LogoutButton
            className="inline-flex items-center gap-2 rounded-lg border border-line/70 px-3 py-2 text-[13px] font-semibold text-slate-700 transition hover:bg-slate-50"
            label={dict.common.logout}
            icon={<LogOut className="h-4 w-4" />}
          />
          <div className="flex items-center gap-3">
            <div className="text-right">
              <p className="text-[13px] font-semibold text-ink">{user.name}</p>
              <p className="text-[11px] uppercase tracking-[0.05em] text-slate-500">
                {user.role}
              </p>
            </div>
            <div className="flex h-8 w-8 items-center justify-center rounded-full border border-slate-200 bg-slate-100 text-xs font-bold text-moss">
              {user.name
                .split(" ")
                .map((part) => part[0])
                .slice(0, 2)
                .join("")}
            </div>
          </div>
        </div>
      </header>
      <div className="border-b border-line/50 bg-white px-6 py-5">
        <h1 className="text-[20px] font-semibold leading-7 text-ink">{title}</h1>
        <p className="mt-1 max-w-3xl text-[13px] leading-[18px] text-slate-500">
          {description}
        </p>
      </div>
    </>
  );
}
