import Link from "next/link";
import { getCurrentUser } from "@/lib/auth/dal";
import { logout } from "@/lib/auth/actions";
import { getUnreadNotificationCount } from "@/lib/notifications/queries";
import { getUnreadMessageCount } from "@/lib/messages/queries";
import { MobileNav } from "@/components/site/MobileNav";

export async function SiteHeader() {
  const user = await getCurrentUser();
  const [unreadCount, unreadMessageCount] = user
    ? await Promise.all([getUnreadNotificationCount(user.id), getUnreadMessageCount(user.id)])
    : [0, 0];

  return (
    <header className="border-b border-border bg-surface/80 backdrop-blur">
      <div className="mx-auto flex max-w-6xl items-center justify-between gap-4 px-4 py-4 sm:gap-6 sm:px-6">
        <Link href="/" className="flex items-center gap-2.5">
          <span className="flex h-9 w-9 items-center justify-center rounded-[10px] bg-gradient-to-br from-accent to-accent-2">
            <svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="white" strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round">
              <path d="M4 5h16a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H9l-5 4v-4H4a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1z" />
            </svg>
          </span>
          <span className="font-heading text-[19px] sm:text-[21px]">
            Sohbete<span className="text-accent">Gir</span>
          </span>
        </Link>

        <div className="hidden items-center gap-7 md:flex">
          <Link href="/uyeler" className="text-[14.5px] font-semibold text-text-muted hover:text-accent">
            Üyeler
          </Link>
          <Link href="/itiraflar" className="text-[14.5px] font-semibold text-text-muted hover:text-accent">
            İtiraflar
          </Link>
          <Link href="/sozler" className="text-[14.5px] font-semibold text-text-muted hover:text-accent">
            Güzel Sözler
          </Link>
          <Link href="/odalar" className="text-[14.5px] font-semibold text-text-muted hover:text-accent">
            Odalar
          </Link>
        </div>

        <div className="hidden items-center gap-3 md:flex">
          <Link
            href="/iletisim"
            title="İletişim"
            className="p-2 text-text-muted hover:text-accent"
          >
            <svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round">
              <rect x="3" y="5" width="18" height="14" rx="2" />
              <path d="M3 7l9 6 9-6" />
            </svg>
          </Link>
          <Link
            href="/itiraflar"
            className="flex items-center gap-1.5 rounded-full border border-border px-4 py-2.5 text-[13.5px] font-semibold text-text hover:border-accent hover:text-accent"
          >
            <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.9} strokeLinecap="round" strokeLinejoin="round">
              <rect x="5" y="11" width="14" height="9" rx="2" />
              <path d="M8 11V7a4 4 0 1 1 8 0v4" />
            </svg>
            İtiraf Et
          </Link>
          {user ? (
            <>
              <Link href="/mesajlar" className="relative p-2 text-text-muted hover:text-accent">
                <svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round">
                  <path d="M4 5h16a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H9l-5 4v-4H4a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1z" />
                </svg>
                {unreadMessageCount > 0 && (
                  <span className="absolute right-0.5 top-0.5 flex h-4 min-w-4 items-center justify-center rounded-full bg-accent px-1 text-[10px] font-bold text-white">
                    {unreadMessageCount}
                  </span>
                )}
              </Link>
              <Link href="/bildirimler" className="relative p-2 text-text-muted hover:text-accent">
                <svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8} strokeLinecap="round" strokeLinejoin="round">
                  <path d="M12 3a5 5 0 0 0-5 5v3.6c0 .6-.2 1.2-.6 1.7L5 15h14l-1.4-1.7c-.4-.5-.6-1.1-.6-1.7V8a5 5 0 0 0-5-5z" />
                  <path d="M9.5 18a2.5 2.5 0 0 0 5 0" />
                </svg>
                {unreadCount > 0 && (
                  <span className="absolute right-0.5 top-0.5 flex h-4 min-w-4 items-center justify-center rounded-full bg-accent px-1 text-[10px] font-bold text-white">
                    {unreadCount}
                  </span>
                )}
              </Link>
              <Link href="/profil" className="text-[14.5px] font-semibold text-text-muted hover:text-accent">
                Profilim
              </Link>
              <form action={logout}>
                <button className="rounded-full bg-accent px-6 py-2.5 text-[14.5px] font-semibold text-white hover:bg-accent-dark">
                  Çıkış Yap
                </button>
              </form>
            </>
          ) : (
            <>
              <Link href="/giris" className="px-3 py-2.5 text-[14.5px] font-semibold text-text-muted hover:text-accent">
                Giriş Yap
              </Link>
              <Link
                href="/kayit"
                className="rounded-full bg-accent px-6 py-2.5 text-[14.5px] font-semibold text-white hover:bg-accent-dark"
              >
                Ücretsiz Üye Ol
              </Link>
            </>
          )}
        </div>

        <MobileNav
          isAuthenticated={Boolean(user)}
          unreadCount={unreadCount}
          unreadMessageCount={unreadMessageCount}
          logoutAction={logout}
        />
      </div>
    </header>
  );
}
