"use client";

import { Bell, Coins, Crown, Gift } from "lucide-react";
import { useSessionStore } from "@/lib/session-store";
import { useUIStore } from "@/lib/ui-store";
import { useNotifStore } from "@/lib/notification-store";
import { useI18n } from "@/lib/i18n";
import { Avatar } from "./Avatar";

export function TopBar() {
  const profile = useSessionStore((s) => s.profile);
  const go = useUIStore((s) => s.go);
  const openDaily = useUIStore((s) => s.openDaily);
  const unread = useNotifStore((s) => s.unread);
  const { t } = useI18n();
  if (!profile) return null;

  return (
    <div className="flex items-center justify-between gap-3">
      <button
        onClick={() => go("profile")}
        className="glass rounded-full ltr:pr-4 rtl:pl-4 ltr:pl-1.5 rtl:pr-1.5 py-1.5 flex items-center gap-2 hover:bg-navy-800/80 transition"
      >
        <span className="relative size-9 rounded-full bg-navy-900 gold-border flex items-center justify-center overflow-hidden">
          <Avatar id={profile.avatar} image={profile.avatarImage} size={profile.avatarImage ? 36 : 26} />
        </span>
        <span className="text-start leading-tight">
          <span className="flex items-center gap-1 text-sm font-bold text-cream max-w-28 truncate">
            {profile.displayName}
            {profile.plan === "pro" && <Crown className="size-3 text-gold-400 fill-gold-500 shrink-0" />}
          </span>
          <span className="block text-[10px] text-gold-400/80">
            {t("common.level")} {profile.level}
          </span>
        </span>
      </button>

      <div className="flex items-center gap-2">
        <button
          onClick={() => go("notifications")}
          className="glass rounded-full p-2 hover:bg-navy-800/80 transition relative"
          title={t("notif.title")}
        >
          <Bell className="size-4 text-gold-400" />
          {unread > 0 && (
            <span className="absolute -top-0.5 ltr:-right-0.5 rtl:-left-0.5 min-w-4 h-4 px-1 rounded-full bg-rose-500 text-[9px] font-bold text-white flex items-center justify-center">
              {unread > 9 ? "9+" : unread}
            </span>
          )}
        </button>
        <button
          onClick={openDaily}
          className="glass rounded-full p-2 hover:bg-navy-800/80 transition"
          title={t("daily.title")}
        >
          <Gift className="size-4 text-gold-400" />
        </button>
        <div className="glass rounded-full px-3 py-1.5 flex items-center gap-1.5">
          <Coins className="size-4 text-gold-400" />
          <span className="text-sm font-bold text-cream tabular-nums">
            {profile.coins.toLocaleString()}
          </span>
        </div>
      </div>
    </div>
  );
}
