import * as vscode from 'vscode';
import { ApprovalQueue } from './approvalQueue';

/**
 * Status-bar badge that pulses while an approval is pending. Clicking it
 * focuses the Approval Panel webview view. Hidden when nothing is pending.
 *
 * Lives separately from `src/core/statusBar.ts` (agent run-status indicator)
 * because the two states change for completely different reasons and the
 * single-item statusBarManager would lose the agent-status during a long
 * dry-run review otherwise.
 */
export class ApprovalStatusBar implements vscode.Disposable {
    private readonly _item: vscode.StatusBarItem;
    private readonly _sub: vscode.Disposable;
    public static readonly focusCommand = 'g1nation.approval.focus';

    constructor(private readonly _queue: ApprovalQueue) {
        this._item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 95);
        this._item.command = ApprovalStatusBar.focusCommand;
        this._item.tooltip = 'Astra: 승인 대기 중인 작업이 있습니다. 클릭해서 검토하세요.';
        this._sub = this._queue.onChange(() => this._refresh());
        this._refresh();
    }

    private _refresh(): void {
        const count = this._queue.pendingCount();
        if (count === 0) {
            this._item.hide();
            return;
        }
        this._item.text = `$(warning) 승인 대기 ${count}`;
        this._item.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
        this._item.show();
    }

    dispose(): void {
        this._sub.dispose();
        this._item.dispose();
    }
}
