import { useQuery } from "@tanstack/react-query";
import { api } from "../lib/api";
import type { PlayerSnapshot } from "../lib/playerProgress";

export function usePlayerProgress(): {
  player: PlayerSnapshot | null;
  isLoading: boolean;
  isError: boolean;
} {
  const q = useQuery({
    queryKey: ["player-state"],
    queryFn: async () => {
      const res = await api.playerState();
      const { ok: _ok, updatedAt: _at, ...player } = res;
      return player as PlayerSnapshot;
    },
    staleTime: 15_000,
    refetchOnWindowFocus: true,
  });

  return {
    player: q.data ?? null,
    isLoading: q.isLoading,
    isError: q.isError,
  };
}