import type { Encounter } from "@initiative/domain";
import { Library, StepBack, StepForward, Trash2 } from "lucide-react";
import { D20Icon } from "./d20-icon";
import { Button } from "./ui/button";
import { ConfirmButton } from "./ui/confirm-button";

interface TurnNavigationProps {
	encounter: Encounter;
	onAdvanceTurn: () => void;
	onRetreatTurn: () => void;
	onClearEncounter: () => void;
	onRollAllInitiative: () => void;
	onOpenSourceManager: () => void;
}

export function TurnNavigation({
	encounter,
	onAdvanceTurn,
	onRetreatTurn,
	onClearEncounter,
	onRollAllInitiative,
	onOpenSourceManager,
}: TurnNavigationProps) {
	const hasCombatants = encounter.combatants.length > 0;
	const isAtStart = encounter.roundNumber === 1 && encounter.activeIndex === 0;
	const activeCombatant = encounter.combatants[encounter.activeIndex];

	return (
		<div className="flex items-center gap-3 rounded-md border border-border bg-card px-4 py-3">
			<Button
				size="icon"
				onClick={onRetreatTurn}
				disabled={!hasCombatants || isAtStart}
				title="Previous turn"
				aria-label="Previous turn"
			>
				<StepBack className="h-5 w-5" />
			</Button>

			<div className="min-w-0 flex-1 flex items-center justify-center gap-2 text-sm">
				<span className="rounded-full bg-muted text-foreground text-sm px-2 py-0.5 font-semibold shrink-0">
					R{encounter.roundNumber}
				</span>
				{activeCombatant ? (
					<span className="truncate font-medium">{activeCombatant.name}</span>
				) : (
					<span className="text-muted-foreground">No combatants</span>
				)}
			</div>

			<div className="flex flex-shrink-0 items-center gap-3">
				<div className="flex items-center gap-0">
					<Button
						variant="ghost"
						size="icon"
						className="text-muted-foreground hover:text-hover-action"
						onClick={onRollAllInitiative}
						title="Roll all initiative"
						aria-label="Roll all initiative"
					>
						<D20Icon className="h-6 w-6" />
					</Button>
					<Button
						variant="ghost"
						size="icon"
						className="text-muted-foreground hover:text-hover-neutral"
						onClick={onOpenSourceManager}
						title="Manage cached sources"
						aria-label="Manage cached sources"
					>
						<Library className="h-5 w-5" />
					</Button>
					<ConfirmButton
						icon={<Trash2 className="h-5 w-5" />}
						label="Clear encounter"
						onConfirm={onClearEncounter}
						disabled={!hasCombatants}
						className="text-muted-foreground"
					/>
				</div>
				<Button
					size="icon"
					onClick={onAdvanceTurn}
					disabled={!hasCombatants}
					title="Next turn"
					aria-label="Next turn"
				>
					<StepForward className="h-5 w-5" />
				</Button>
			</div>
		</div>
	);
}
