import { AgentPanel } from "./components/AgentPanel";
import { EventFeedPanel } from "./components/EventFeedPanel";
import { GameOverOverlay } from "./components/GameOverOverlay";
import { ScoreboardHeader } from "./components/ScoreboardHeader";
import { SimulationControls } from "./components/SimulationControls";
import { StatusToasts } from "./components/StatusToasts";
import { useWorldSimulation } from "./hooks/useWorldSimulation";
import { WorldSimulatorScene } from "./scene/WorldSimulatorScene";

export function App() {
  const simulation = useWorldSimulation();
  const gameStatus = simulation.snapshot?.simulation.game_status ?? "running";

  return (
    <main className="shell">
      <WorldSimulatorScene
        snapshot={simulation.snapshot}
        selectedId={simulation.selectedId}
        onSelectEntity={simulation.setSelectedId}
      />

      <ScoreboardHeader
        snapshot={simulation.snapshot}
        isWaitingForTick={simulation.isWaitingForTick}
      />

      <EventFeedPanel
        events={simulation.eventHistory}
        countries={simulation.snapshot?.countries ?? []}
        entities={simulation.snapshot?.entities ?? []}
      />

      {simulation.hideTickControls ? null : (
        <SimulationControls
          hasSnapshot={simulation.snapshot !== null}
          isAutoTicking={simulation.isAutoTicking}
          isWaitingForTick={simulation.isWaitingForTick}
          isWorldCommandPending={simulation.isWorldCommandPending}
          onStep={() => {
            void simulation.step();
          }}
          onToggleAutoTick={() => {
            simulation.setIsAutoTicking(!simulation.isAutoTicking);
          }}
        />
      )}

      <AgentPanel
        entity={simulation.selectedEntity}
        beast={simulation.selectedBeast}
        resource={simulation.selectedResource}
        house={simulation.selectedHouse}
        entities={simulation.snapshot?.entities ?? []}
        onSelectEntity={simulation.setSelectedId}
      />

      <StatusToasts
        error={simulation.error}
        isWaitingForTick={simulation.isWaitingForTick}
      />

      {simulation.snapshot && gameStatus !== "running" ? (
        <GameOverOverlay snapshot={simulation.snapshot} events={simulation.eventHistory} />
      ) : null}
    </main>
  );
}
