/// Gateway management panel shown when huskies runs in `--gateway` mode.
///
/// Provides:
/// - A cross-project pipeline status view showing active stories per project.
/// - Clicking a project card switches to it.
/// - An "Add Agent" button that generates a one-time join token.
/// - Instructions for running a build agent with the token.
/// - A list of connected agents with per-agent status, project assignment, and "Remove" buttons.
/// - Auto-refresh every 5 seconds so new agents and disconnections appear without a page reload.

import * as React from "react";
import {
	gatewayApi,
	type JoinedAgent,
	type GatewayProject,
	type AllProjectsPipeline,
	type PipelineItem,
} from "../api/gateway";

const { useCallback, useEffect, useRef, useState } = React;

/// Seconds of silence before an agent is considered disconnected.
const DISCONNECT_THRESHOLD_SECS = 60;

/// Poll the agent list this often (milliseconds).
const POLL_INTERVAL_MS = 5_000;

type AgentStatus = "idle" | "working" | "disconnected";

/// Derive an agent's display status from its last-seen timestamp and project assignment.
function agentStatus(agent: JoinedAgent): AgentStatus {
	const nowSecs = Date.now() / 1000;
	if (nowSecs - agent.last_seen > DISCONNECT_THRESHOLD_SECS) {
		return "disconnected";
	}
	return agent.assigned_project ? "working" : "idle";
}

const STATUS_COLORS: Record<AgentStatus, string> = {
	idle: "#6e7681",
	working: "#3fb950",
	disconnected: "#f85149",
};

const STATUS_LABELS: Record<AgentStatus, string> = {
	idle: "Idle",
	working: "Working",
	disconnected: "Disconnected",
};

const STAGE_COLORS: Record<string, string> = {
	current: "#3fb950",
	qa: "#d2a679",
	merge: "#79c0ff",
	done: "#6e7681",
};

const STAGE_LABELS: Record<string, string> = {
	current: "In Progress",
	qa: "QA",
	merge: "Merging",
	done: "Done",
};

/// A single story row inside a project pipeline card.
function StoryRow({ item }: { item: PipelineItem }) {
	const color = STAGE_COLORS[item.stage] ?? "#8b949e";
	const label = STAGE_LABELS[item.stage] ?? item.stage;

	return (
		<div
			style={{
				display: "flex",
				alignItems: "center",
				gap: "8px",
				padding: "4px 0",
				fontSize: "0.82em",
			}}
		>
			<span
				style={{
					padding: "1px 6px",
					borderRadius: "10px",
					background: `${color}22`,
					color,
					border: `1px solid ${color}44`,
					whiteSpace: "nowrap",
					flexShrink: 0,
				}}
			>
				{label}
			</span>
			<span style={{ color: "#e6edf3", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
				{item.name}
			</span>
		</div>
	);
}

/// Pipeline status card for a single project.
function ProjectPipelineCard({
	name,
	pipeline,
	isActive,
	onSwitch,
}: {
	name: string;
	pipeline: AllProjectsPipeline["projects"][string];
	isActive: boolean;
	onSwitch: (name: string) => void;
}) {
	const activeItems = pipeline.active ?? [];
	const backlogCount = pipeline.backlog_count ?? 0;
	const hasError = Boolean(pipeline.error);

	return (
		<div
			data-testid={`pipeline-card-${name}`}
			onClick={() => onSwitch(name)}
			style={{
				padding: "12px 16px",
				background: "#161b22",
				border: `1px solid ${isActive ? "#238636" : "#30363d"}`,
				borderRadius: "8px",
				marginBottom: "8px",
				cursor: "pointer",
			}}
		>
			<div
				style={{
					display: "flex",
					alignItems: "center",
					gap: "8px",
					marginBottom: activeItems.length > 0 ? "8px" : 0,
				}}
			>
				<span style={{ fontWeight: 600, color: "#e6edf3" }}>{name}</span>
				{isActive && (
					<span
						style={{
							fontSize: "0.7em",
							padding: "1px 6px",
							borderRadius: "10px",
							background: "#23863622",
							color: "#3fb950",
							border: "1px solid #23863644",
						}}
					>
						active
					</span>
				)}
				<span style={{ marginLeft: "auto", fontSize: "0.75em", color: "#6e7681" }}>
					{backlogCount > 0 ? `${backlogCount} in backlog` : ""}
				</span>
			</div>

			{hasError ? (
				<div style={{ fontSize: "0.8em", color: "#f85149" }}>{pipeline.error}</div>
			) : activeItems.length === 0 ? (
				<div style={{ fontSize: "0.8em", color: "#6e7681" }}>No active stories</div>
			) : (
				<div>
					{activeItems.map((item) => (
						<StoryRow key={item.story_id} item={item} />
					))}
				</div>
			)}
		</div>
	);
}

function TokenDisplay({ token }: { token: string }) {
	const [copied, setCopied] = useState(false);

	const envCmd = `HUSKIES_JOIN_TOKEN=${token} huskies agent --rendezvous <CRDT_SYNC_URL>`;
	const flagCmd = `huskies agent --rendezvous <CRDT_SYNC_URL> --join-token ${token}`;

	const copyToClipboard = useCallback((text: string) => {
		void navigator.clipboard.writeText(text).then(() => {
			setCopied(true);
			setTimeout(() => setCopied(false), 2000);
		});
	}, []);

	return (
		<div
			style={{
				marginTop: "12px",
				padding: "12px 16px",
				background: "#161b22",
				border: "1px solid #238636",
				borderRadius: "8px",
				fontSize: "0.85em",
			}}
		>
			<div style={{ color: "#3fb950", fontWeight: 600, marginBottom: "8px" }}>
				Token generated — run the build agent with one of:
			</div>
			<div style={{ marginBottom: "6px" }}>
				<code
					style={{
						display: "block",
						background: "#0d1117",
						padding: "8px 10px",
						borderRadius: "4px",
						color: "#e6edf3",
						wordBreak: "break-all",
					}}
				>
					{envCmd}
				</code>
			</div>
			<div>
				<code
					style={{
						display: "block",
						background: "#0d1117",
						padding: "8px 10px",
						borderRadius: "4px",
						color: "#e6edf3",
						wordBreak: "break-all",
					}}
				>
					{flagCmd}
				</code>
			</div>
			<button
				type="button"
				onClick={() => copyToClipboard(flagCmd)}
				style={{
					marginTop: "8px",
					fontSize: "0.8em",
					padding: "3px 10px",
					borderRadius: "4px",
					border: "1px solid #444",
					background: "none",
					color: "#aaa",
					cursor: "pointer",
				}}
			>
				{copied ? "Copied!" : "Copy flag command"}
			</button>
			<div style={{ marginTop: "8px", color: "#666", fontSize: "0.85em" }}>
				This token is single-use. Generate a new one for each agent.
			</div>
		</div>
	);
}

function AgentRow({
	agent,
	projects,
	onRemove,
	onAssign,
}: {
	agent: JoinedAgent;
	projects: GatewayProject[];
	onRemove: (id: string) => void;
	onAssign: (id: string, project: string | null) => void;
}) {
	const registeredAt = new Date(agent.registered_at * 1000).toLocaleString();
	const status = agentStatus(agent);
	const statusColor = STATUS_COLORS[status];
	const statusLabel = STATUS_LABELS[status];

	return (
		<div
			data-testid={`agent-row-${agent.id}`}
			style={{
				display: "flex",
				alignItems: "center",
				gap: "12px",
				padding: "10px 14px",
				background: "#161b22",
				border: "1px solid #30363d",
				borderRadius: "8px",
				marginBottom: "8px",
			}}
		>
			<div
				style={{
					width: "8px",
					height: "8px",
					borderRadius: "50%",
					background: statusColor,
					flexShrink: 0,
				}}
				title={statusLabel}
			/>
			<div style={{ flex: 1 }}>
				<div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
					<span style={{ fontWeight: 600, color: "#e6edf3" }}>{agent.label}</span>
					<span
						data-testid={`agent-status-${agent.id}`}
						style={{
							fontSize: "0.75em",
							padding: "1px 6px",
							borderRadius: "10px",
							background: `${statusColor}22`,
							color: statusColor,
							border: `1px solid ${statusColor}44`,
						}}
					>
						{statusLabel}
					</span>
				</div>
				<div style={{ fontSize: "0.8em", color: "#8b949e" }}>
					{agent.address}
				</div>
				<div style={{ fontSize: "0.75em", color: "#6e7681" }}>
					Registered {registeredAt}
					{agent.assigned_project && (
						<span style={{ marginLeft: "8px", color: "#8b949e" }}>
							· Project: {agent.assigned_project}
						</span>
					)}
				</div>
			</div>
			<select
				data-testid={`assign-agent-${agent.id}`}
				value={agent.assigned_project ?? ""}
				onChange={(e) =>
					onAssign(agent.id, e.target.value === "" ? null : e.target.value)
				}
				style={{
					fontSize: "0.8em",
					padding: "4px 8px",
					borderRadius: "4px",
					border: "1px solid #30363d",
					background: "#0d1117",
					color: "#e6edf3",
					cursor: "pointer",
				}}
			>
				<option value="">— unassigned —</option>
				{projects.map((p) => (
					<option key={p.name} value={p.name}>
						{p.name}
					</option>
				))}
			</select>
			<button
				type="button"
				data-testid={`remove-agent-${agent.id}`}
				onClick={() => onRemove(agent.id)}
				style={{
					fontSize: "0.8em",
					padding: "4px 10px",
					borderRadius: "4px",
					border: "1px solid #f85149",
					background: "none",
					color: "#f85149",
					cursor: "pointer",
				}}
			>
				Remove
			</button>
		</div>
	);
}

/// Gateway management panel — rendered when running in `--gateway` mode.
export function GatewayPanel() {
	const [agents, setAgents] = useState<JoinedAgent[]>([]);
	const [projects, setProjects] = useState<GatewayProject[]>([]);
	const [token, setToken] = useState<string | null>(null);
	const [generating, setGenerating] = useState(false);
	const [error, setError] = useState<string | null>(null);
	const [pipeline, setPipeline] = useState<AllProjectsPipeline | null>(null);

	// Keep stable refs so polling intervals don't recreate on state changes.
	const setAgentsRef = useRef(setAgents);
	setAgentsRef.current = setAgents;
	const setPipelineRef = useRef(setPipeline);
	setPipelineRef.current = setPipeline;

	useEffect(() => {
		// Initial load.
		gatewayApi
			.listAgents()
			.then(setAgents)
			.catch(() => setAgents([]));
		gatewayApi
			.getGatewayInfo()
			.then((info) => setProjects(info.projects))
			.catch(() => setProjects([]));
		gatewayApi
			.getAllProjectsPipeline()
			.then(setPipeline)
			.catch(() => setPipeline(null));

		// Poll so the dashboard auto-updates as agents connect/disconnect and
		// stories move through pipelines.
		const timer = setInterval(() => {
			gatewayApi
				.listAgents()
				.then((updated) => setAgentsRef.current(updated))
				.catch(() => {});
			gatewayApi
				.getAllProjectsPipeline()
				.then((updated) => setPipelineRef.current(updated))
				.catch(() => {});
		}, POLL_INTERVAL_MS);

		return () => clearInterval(timer);
	}, []);

	const handleAddAgent = useCallback(async () => {
		setGenerating(true);
		setError(null);
		setToken(null);
		try {
			const result = await gatewayApi.generateToken();
			setToken(result.token);
		} catch (e) {
			setError(e instanceof Error ? e.message : String(e));
		} finally {
			setGenerating(false);
		}
	}, []);

	const handleRemoveAgent = useCallback(async (id: string) => {
		try {
			await gatewayApi.removeAgent(id);
			setAgents((prev) => prev.filter((a) => a.id !== id));
		} catch (e) {
			setError(e instanceof Error ? e.message : String(e));
		}
	}, []);

	const handleAssignAgent = useCallback(
		async (id: string, project: string | null) => {
			try {
				const updated = await gatewayApi.assignAgent(id, project);
				setAgents((prev) =>
					prev.map((a) => (a.id === updated.id ? updated : a)),
				);
			} catch (e) {
				setError(e instanceof Error ? e.message : String(e));
			}
		},
		[],
	);

	const handleSwitchProject = useCallback(async (name: string) => {
		setError(null);
		try {
			const result = await gatewayApi.switchProject(name);
			if (!result.ok) {
				setError(result.error ?? "Failed to switch project");
				return;
			}
			// Refresh pipeline to reflect new active project.
			const updated = await gatewayApi.getAllProjectsPipeline();
			setPipeline(updated);
		} catch (e) {
			setError(e instanceof Error ? e.message : String(e));
		}
	}, []);


	return (
		<div
			style={{
				minHeight: "100vh",
				background: "#0d1117",
				color: "#e6edf3",
				padding: "32px",
				fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif",
			}}
		>
			<div style={{ maxWidth: "720px", margin: "0 auto" }}>
				<h1 style={{ fontSize: "1.5em", fontWeight: 700, marginBottom: "4px" }}>
					Huskies Gateway
				</h1>
				<p style={{ color: "#8b949e", marginBottom: "32px" }}>
					Manage build agents connected to this gateway.
				</p>

				{/* Cross-project pipeline status */}
				<section style={{ marginBottom: "32px" }}>
					<h2
						style={{
							fontSize: "1.1em",
							fontWeight: 600,
							marginBottom: "12px",
							borderBottom: "1px solid #21262d",
							paddingBottom: "8px",
						}}
					>
						Pipeline Status
					</h2>
					{pipeline ? (
						Object.entries(pipeline.projects).map(([name, status]) => (
							<ProjectPipelineCard
								key={name}
								name={name}
								pipeline={status}
								isActive={name === pipeline.active}
								onSwitch={handleSwitchProject}
							/>
						))
					) : (
						<p style={{ color: "#6e7681" }}>Loading pipeline status…</p>
					)}
				</section>

				{/* Add Agent */}
				<section style={{ marginBottom: "32px" }}>
					<h2
						style={{
							fontSize: "1.1em",
							fontWeight: 600,
							marginBottom: "12px",
							borderBottom: "1px solid #21262d",
							paddingBottom: "8px",
						}}
					>
						Add Agent
					</h2>
					<button
						type="button"
						data-testid="add-agent-button"
						onClick={handleAddAgent}
						disabled={generating}
						style={{
							padding: "8px 18px",
							borderRadius: "6px",
							border: "1px solid #238636",
							background: generating ? "#1a2f1a" : "#238636",
							color: "#fff",
							cursor: generating ? "not-allowed" : "pointer",
							fontWeight: 600,
							fontSize: "0.9em",
						}}
					>
						{generating ? "Generating…" : "Add Agent"}
					</button>
					{token && <TokenDisplay token={token} />}
				</section>

				{/* Agent list */}
				<section>
					<h2
						style={{
							fontSize: "1.1em",
							fontWeight: 600,
							marginBottom: "12px",
							borderBottom: "1px solid #21262d",
							paddingBottom: "8px",
						}}
					>
						Connected Agents{" "}
						{agents.length > 0 && (
							<span
								style={{
									fontSize: "0.8em",
									color: "#8b949e",
									fontWeight: 400,
								}}
							>
								({agents.length})
							</span>
						)}
					</h2>
					{agents.length === 0 ? (
						<p style={{ color: "#6e7681" }}>
							No agents connected yet. Click "Add Agent" to generate a join
							token.
						</p>
					) : (
						<div>
							{agents.map((agent) => (
								<AgentRow
									key={agent.id}
									agent={agent}
									projects={projects}
									onRemove={handleRemoveAgent}
									onAssign={handleAssignAgent}
								/>
							))}
						</div>
					)}
				</section>

				{/* Project management */}
				<section style={{ marginTop: "32px" }}>
					<h2
						style={{
							fontSize: "1.1em",
							fontWeight: 600,
							marginBottom: "12px",
							borderBottom: "1px solid #21262d",
							paddingBottom: "8px",
						}}
					>
						Projects{" "}
						{projects.length > 0 && (
							<span style={{ fontSize: "0.8em", color: "#8b949e", fontWeight: 400 }}>
								({projects.length})
							</span>
						)}
					</h2>

					{/* Existing projects list */}
					{projects.map((p) => (
						<div
							key={p.name}
							data-testid={`project-row-${p.name}`}
							style={{
								display: "flex",
								alignItems: "center",
								gap: "12px",
								padding: "10px 14px",
								background: "#161b22",
								border: "1px solid #30363d",
								borderRadius: "8px",
								marginBottom: "8px",
							}}
						>
							<div style={{ flex: 1 }}>
								<div style={{ fontWeight: 600, color: "#e6edf3" }}>{p.name}</div>
								<div style={{ fontSize: "0.8em", color: "#8b949e" }}>{p.url}</div>
							</div>
							</div>
					))}
				</section>

				{error && (
					<div
						style={{
							marginTop: "16px",
							padding: "10px 14px",
							background: "#f8514911",
							border: "1px solid #f85149",
							borderRadius: "6px",
							color: "#f85149",
							fontSize: "0.875em",
						}}
					>
						{error}
					</div>
				)}
			</div>
		</div>
	);
}
