"use client";

import { useState, useCallback } from "react";
import { Users, QrCode, LogOut, Crown, Lock } from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";
import QrInviteModal from "./QrInviteModal";
import RoomManageModal from "./RoomManageModal";
import type { UserProfile, SceneType } from "@/types";
import { getSceneConfig } from "@/lib/sceneConfig";

interface TopNavProps {
  roomId: string;
  userCount: number;
  onExit?: () => void;
  isCreator?: boolean;
  userId?: string;
  users?: string[];
  locked?: boolean;
  swipeCounts?: Record<string, number>;
  totalCards?: number;
  userProfiles?: Record<string, UserProfile>;
  scene?: SceneType;
}

export default function TopNav({
  roomId,
  userCount,
  onExit,
  isCreator = false,
  userId = "",
  users = [],
  locked = false,
  swipeCounts = {},
  totalCards = 0,
  userProfiles = {},
  scene = "eat",
}: TopNavProps) {
  const sceneConfig = getSceneConfig(scene);
  const [toast, setToast] = useState("");
  const [showQr, setShowQr] = useState(false);
  const [showManage, setShowManage] = useState(false);

  const showToast = useCallback((msg: string) => {
    setToast(msg);
    setTimeout(() => setToast(""), 2200);
  }, []);

  return (
    <>
      <nav className="relative z-10 flex h-14 items-center justify-between px-4">
        <div className="flex items-center gap-1.5">
          <button
            onClick={() => setShowQr(true)}
            className="flex items-center gap-1 rounded-full bg-accent/15 px-2.5 py-1 text-xs font-semibold text-accent transition-colors active:bg-accent/25"
          >
            <QrCode size={13} />
            邀请
          </button>
          {isCreator && (
            <button
              onClick={() => setShowManage(true)}
              className="flex items-center gap-1 rounded-full bg-amber-500/15 px-2.5 py-1 text-xs font-semibold text-amber-400 transition-colors active:bg-amber-500/25"
            >
              <Crown size={13} />
              管理
            </button>
          )}
        </div>

        <h1 className="text-center text-base font-bold tracking-tight text-white">
          <span className="block leading-tight">NoWhatever</span>
          <span className="block text-[10px] font-medium tracking-widest text-muted">
            别说随便
          </span>
        </h1>

        <div className="flex items-center justify-end gap-1.5 text-xs text-muted">
          {locked && (
            <Lock size={12} className="text-amber-500" />
          )}
          <span className="rounded-full bg-surface px-2 py-0.5 font-medium text-gray-400">
            {roomId}
          </span>
          <div className="flex items-center gap-0.5">
            <Users size={13} />
            <span className="font-semibold text-accent">{userCount}</span>
          </div>
          <button
            onClick={onExit}
            className="ml-1 flex items-center justify-center rounded-full p-1 text-muted transition-colors active:bg-elevated active:text-gray-300"
            aria-label="退出房间"
          >
            <LogOut size={15} />
          </button>
        </div>
      </nav>

      <AnimatePresence>
        {toast && (
          <motion.div
            className="fixed left-1/2 top-16 z-50 -translate-x-1/2 rounded-xl bg-elevated px-4 py-2.5 text-xs font-medium text-white shadow-lg ring-1 ring-subtle"
            initial={{ opacity: 0, y: -12, x: "-50%" }}
            animate={{ opacity: 1, y: 0, x: "-50%" }}
            exit={{ opacity: 0, y: -12, x: "-50%" }}
            transition={{ type: "spring", stiffness: 400, damping: 25 }}
          >
            {toast}
          </motion.div>
        )}
      </AnimatePresence>

      <QrInviteModal
        open={showQr}
        onClose={() => setShowQr(false)}
        roomId={roomId}
        onToast={showToast}
        scene={scene}
      />

      {isCreator && (
        <RoomManageModal
          open={showManage}
          onClose={() => setShowManage(false)}
          roomId={roomId}
          userId={userId}
          users={users}
          locked={locked}
          swipeCounts={swipeCounts}
          totalCards={totalCards}
          userProfiles={userProfiles}
          onToast={showToast}
        />
      )}
    </>
  );
}
