import type { Component } from "../tui.js";

/**
 * Snapshot for the autonomous-mode strip above main TUI content.
 */
export type AutonomousModeStatus = {
	currentSlice?: string;
	sliceStatus?: string;
	progress?: number;
	totalTasks?: number;
	completedTasks?: number;
};

// Named ANSI color constants for the autonomous status bar
const DIM = "\x1b[90m";
const BOLD_WHITE = "\x1b[97m";
const CYAN = "\x1b[96m";
const GREEN = "\x1b[92m";
const YELLOW = "\x1b[93m";
const MAGENTA = "\x1b[95m";
const RESET = "\x1b[0m";

/**
 * Renders the autonomous-mode status bar as a single terminal line.
 *
 * Purpose: isolate autonomous chrome from `TUI` so `tui.ts` stays smaller and
 * this strip can evolve independently (copy, colors, layout).
 */
export class AutonomousStatusBar implements Component {
	private status: AutonomousModeStatus | undefined;

	setStatus(status: AutonomousModeStatus | undefined): void {
		this.status = status;
	}

	getStatus(): AutonomousModeStatus | undefined {
		return this.status;
	}

	invalidate(): void {}

	/** Format a single status value with ANSI cyan highlighting. */
	private renderAutonomousStatus(status: string): string {
		return `${CYAN}${status}${RESET}`;
	}

	render(width: number): string[] {
		if (!this.status) return [];

		const { currentSlice, sliceStatus, progress, totalTasks, completedTasks } =
			this.status;
		const noColor = process.env.NO_COLOR === "1";

		let statusLine = noColor
			? "│ AUTONOMOUS MODE "
			: `${DIM}│ AUTONOMOUS MODE `;

		if (currentSlice) {
			statusLine += noColor
				? `Slice: ${currentSlice} `
				: `${BOLD_WHITE}Slice: ${this.renderAutonomousStatus(currentSlice)} `;
		}

		if (sliceStatus) {
			statusLine += noColor
				? `Status: ${sliceStatus} `
				: `${BOLD_WHITE}Status: ${GREEN}${sliceStatus} `;
		}

		if (progress !== undefined) {
			const progressBar = createProgressBar(progress, width - 30);
			statusLine += noColor
				? `Progress: ${progressBar} `
				: `${BOLD_WHITE}Progress: ${YELLOW}${progressBar} `;
		}

		if (totalTasks !== undefined && completedTasks !== undefined) {
			statusLine += noColor
				? `Tasks: ${completedTasks}/${totalTasks} `
				: `${BOLD_WHITE}Tasks: ${MAGENTA}${completedTasks}/${totalTasks} `;
		}

		statusLine += noColor ? "│" : `${DIM}│${RESET}`;
		return [statusLine];
	}
}

function createProgressBar(progress: number, width: number): string {
	const barWidth = Math.min(20, Math.max(5, width));
	const filled = Math.floor((progress / 100) * barWidth);
	const empty = barWidth - filled;
	return `[${"█".repeat(filled)}${"░".repeat(empty)}] ${progress}%`;
}
