import { Link } from '@tanstack/react-router';
import { useSessionStore } from '@/stores/sessionStore';
import { leaveRoom } from '@/lib/sync/wsSync';

/** Header chip shown while connected as a player, with a link back to the table + Leave. */
export function PlayerSessionBadge() {
  const intent = useSessionStore((s) => s.joinIntent);
  const status = useSessionStore((s) => s.status);
  const role = useSessionStore((s) => s.role);
  if (!intent || role === 'gm') return null;
  const label = status === 'connected' ? 'Live' : status === 'connecting' ? 'Connecting…' : status;
  return (
    <div data-testid="player-live" className="flex items-center gap-1 rounded-md border border-success/40 bg-success/5 px-2 py-1 text-xs">
      <span className={status === 'connected' ? 'text-success' : 'text-muted'}>● {label}</span>
      <Link to="/play" className="font-mono font-semibold text-accent" title="Open the table">{intent.joinCode}</Link>
      <button onClick={leaveRoom} className="px-1 text-danger" title="Leave session">✕</button>
    </div>
  );
}
