#!/usr/bin/env bash
# ============================================================================
# BLACKROAD OS, INC. - PROPRIETARY AND CONFIDENTIAL
# Copyright (c) 2025-2026 BlackRoad OS, Inc. All Rights Reserved.
# ============================================================================
# br-dashboard - BlackRoad Dashboard CLI

set -eo pipefail

PINK='\033[38;5;205m'
AMBER='\033[38;5;214m'
GREEN='\033[38;5;82m'
BLUE='\033[38;5;69m'
RED='\033[38;5;196m'
NC='\033[0m'

DASHBOARD_DIR="$HOME/.blackroad/dashboard"
PORT=${DASHBOARD_PORT:-3000}

cmd="${1:-help}"
shift 2>/dev/null

case "$cmd" in
    start)
        mkdir -p "$DASHBOARD_DIR"
        # Check if already running
        if [[ -f "$DASHBOARD_DIR/dashboard.pid" ]]; then
            local pid
            pid=$(cat "$DASHBOARD_DIR/dashboard.pid")
            if kill -0 "$pid" 2>/dev/null; then
                echo -e "${GREEN}●${NC} Dashboard already running (PID $pid)"
                exit 0
            fi
            rm -f "$DASHBOARD_DIR/dashboard.pid"
        fi
        if [[ ! -f "$DASHBOARD_DIR/dashboard_server.py" ]]; then
            echo -e "${RED}✗${NC} Dashboard server not found at $DASHBOARD_DIR/dashboard_server.py"
            exit 1
        fi
        echo -e "${PINK}Starting BlackRoad Dashboard...${NC}"
        nohup python3 "$DASHBOARD_DIR/dashboard_server.py" "$PORT" > "$DASHBOARD_DIR/dashboard.log" 2>&1 &
        echo $! > "$DASHBOARD_DIR/dashboard.pid"
        sleep 1
        local pid
        pid=$(cat "$DASHBOARD_DIR/dashboard.pid")
        if kill -0 "$pid" 2>/dev/null; then
            echo -e "${GREEN}✓${NC} Dashboard running on http://localhost:$PORT"
            echo -e "  PID: $pid"
        else
            echo -e "${RED}✗${NC} Failed to start dashboard"
            rm -f "$DASHBOARD_DIR/dashboard.pid"
            cat "$DASHBOARD_DIR/dashboard.log" 2>/dev/null
            exit 1
        fi
        ;;
    stop)
        if [[ -f "$DASHBOARD_DIR/dashboard.pid" ]]; then
            local pid
            pid=$(cat "$DASHBOARD_DIR/dashboard.pid")
            if kill -0 "$pid" 2>/dev/null; then
                kill "$pid" 2>/dev/null
                echo -e "${AMBER}Dashboard stopped (PID $pid)${NC}"
            else
                echo -e "${AMBER}Dashboard was not running (stale PID)${NC}"
            fi
            rm -f "$DASHBOARD_DIR/dashboard.pid"
        else
            echo "Dashboard not running"
        fi
        ;;
    status)
        if [[ -f "$DASHBOARD_DIR/dashboard.pid" ]]; then
            local pid
            pid=$(cat "$DASHBOARD_DIR/dashboard.pid")
            if kill -0 "$pid" 2>/dev/null; then
                echo -e "${GREEN}●${NC} Dashboard running"
                echo "  PID: $pid"
                echo "  URL: http://localhost:$PORT"
            else
                echo -e "${RED}○${NC} Dashboard not running (stale PID file)"
                rm -f "$DASHBOARD_DIR/dashboard.pid"
            fi
        else
            echo -e "${RED}○${NC} Dashboard not running"
        fi
        ;;
    open)
        if [ -f "$DASHBOARD_DIR/dashboard.pid" ] && kill -0 $(cat "$DASHBOARD_DIR/dashboard.pid") 2>/dev/null; then
            open "http://localhost:$PORT" 2>/dev/null || xdg-open "http://localhost:$PORT" 2>/dev/null || echo "Open http://localhost:$PORT"
        else
            echo "Dashboard not running. Start with: br-dashboard start"
        fi
        ;;
    log|logs)
        if [ -f "$DASHBOARD_DIR/dashboard.log" ]; then
            tail -f "$DASHBOARD_DIR/dashboard.log"
        else
            echo "No log file found"
        fi
        ;;
    help|*)
        echo -e "${PINK}br-dashboard - BlackRoad Dashboard CLI${NC}"
        echo ""
        echo "Commands:"
        echo "  start       Start dashboard server"
        echo "  stop        Stop dashboard server"
        echo "  status      Show dashboard status"
        echo "  open        Open dashboard in browser"
        echo "  logs        Tail dashboard logs"
        echo ""
        echo "Environment:"
        echo "  DASHBOARD_PORT  Port to run on (default: 3000)"
        ;;
esac
