#!/bin/bash
# ============================================
# BreakPilot Status Overview
# ============================================
# Shows current environment, git branch, and service status
#
# Usage: ./scripts/status.sh
# ============================================

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

cd "$ROOT_DIR"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  BreakPilot Status${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Current Environment
echo -e "${CYAN}Environment:${NC}"
if [ -f .env ]; then
    CURRENT_ENV=$(grep "^ENVIRONMENT=" .env 2>/dev/null | cut -d= -f2)
    PROJECT_NAME=$(grep "^COMPOSE_PROJECT_NAME=" .env 2>/dev/null | cut -d= -f2)

    case $CURRENT_ENV in
        development)
            echo -e "  Active: ${GREEN}$CURRENT_ENV${NC}"
            ;;
        staging)
            echo -e "  Active: ${YELLOW}$CURRENT_ENV${NC}"
            ;;
        production)
            echo -e "  Active: ${RED}$CURRENT_ENV${NC}"
            ;;
        *)
            echo -e "  Active: ${CURRENT_ENV:-unknown}"
            ;;
    esac
    echo "  Project: ${PROJECT_NAME:-not set}"
else
    echo -e "  ${YELLOW}Not configured (.env missing)${NC}"
    echo "  Run: ./scripts/env-switch.sh dev"
fi
echo ""

# Git Status
echo -e "${CYAN}Git:${NC}"
if [ -d .git ]; then
    BRANCH=$(git branch --show-current 2>/dev/null || echo "unknown")
    case $BRANCH in
        develop)
            echo -e "  Branch: ${GREEN}$BRANCH${NC} (development)"
            ;;
        staging)
            echo -e "  Branch: ${YELLOW}$BRANCH${NC} (staging)"
            ;;
        main)
            echo -e "  Branch: ${RED}$BRANCH${NC} (production)"
            ;;
        *)
            echo "  Branch: $BRANCH"
            ;;
    esac

    # Check for uncommitted changes
    if git diff-index --quiet HEAD -- 2>/dev/null; then
        echo -e "  Status: ${GREEN}Clean${NC}"
    else
        CHANGES=$(git status --porcelain | wc -l | tr -d ' ')
        echo -e "  Status: ${YELLOW}$CHANGES uncommitted change(s)${NC}"
    fi
else
    echo -e "  ${YELLOW}Not a git repository${NC}"
    echo "  Run: git init"
fi
echo ""

# Docker Status
echo -e "${CYAN}Services:${NC}"
if command -v docker &> /dev/null; then
    RUNNING=$(docker compose ps --format "table {{.Name}}\t{{.Status}}" 2>/dev/null | tail -n +2 | wc -l | tr -d ' ')
    if [ "$RUNNING" -gt 0 ]; then
        echo "  Running: $RUNNING container(s)"
        echo ""
        docker compose ps --format "table {{.Name}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null | head -15

        TOTAL=$(docker compose ps -a --format "{{.Name}}" 2>/dev/null | wc -l | tr -d ' ')
        if [ "$TOTAL" -gt 14 ]; then
            echo "  ... and $((TOTAL - 14)) more"
        fi
    else
        echo -e "  ${YELLOW}No services running${NC}"
        echo "  Start with: ./scripts/start.sh dev"
    fi
else
    echo -e "  ${RED}Docker not available${NC}"
fi
echo ""

# Database Volumes
echo -e "${CYAN}Database Volumes:${NC}"
docker volume ls --format "{{.Name}}" 2>/dev/null | grep -E "(postgres|breakpilot.*postgres)" | while read vol; do
    case $vol in
        *staging*)
            echo -e "  ${YELLOW}$vol${NC}"
            ;;
        *prod*)
            echo -e "  ${RED}$vol${NC}"
            ;;
        *)
            echo -e "  ${GREEN}$vol${NC}"
            ;;
    esac
done || echo "  No database volumes found"
echo ""

# Quick Commands
echo -e "${CYAN}Quick Commands:${NC}"
echo "  Switch env:  ./scripts/env-switch.sh [dev|staging]"
echo "  Start:       ./scripts/start.sh [dev|staging]"
echo "  Stop:        ./scripts/stop.sh [dev|staging]"
echo "  Promote:     ./scripts/promote.sh [dev-to-staging|staging-to-prod]"
echo "  Logs:        docker compose logs -f [service]"
