#!/bin/bash

# ═══════════════════════════════════════════════════════════════════════════
# STATUS.SH - Quick Status Check
# ═══════════════════════════════════════════════════════════════════════════
# 
# Purpose: Show complete system status at a glance
# Created: February 20, 2026 by Chronicler #19
# 
# Usage: ./status.sh
# 
# ═══════════════════════════════════════════════════════════════════════════

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

echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${CYAN}${BOLD}FIREFROST GAMING - QUICK STATUS${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""

# Current Phase
if grep -q "Phase 0.5" docs/core/tasks.md 2>/dev/null; then
    echo -e "${BOLD}Current Phase:${NC} Phase 0.5 (Management Services)"
elif grep -q "Phase 1" docs/core/tasks.md 2>/dev/null; then
    echo -e "${BOLD}Current Phase:${NC} Phase 1 (DDoS Protection)"
else
    echo -e "${BOLD}Current Phase:${NC} Unknown"
fi

# Git Status
if git rev-parse --git-dir > /dev/null 2>&1; then
    BRANCH=$(git branch --show-current)
    echo -e "${BOLD}Git Branch:${NC} $BRANCH"
    
    if git diff-index --quiet HEAD -- 2>/dev/null; then
        echo -e "${BOLD}Working Dir:${NC} ${GREEN}Clean${NC}"
    else
        CHANGED=$(git status --short | wc -l)
        echo -e "${BOLD}Working Dir:${NC} ${YELLOW}$CHANGED file(s) modified${NC}"
    fi
fi

echo ""

# Task Summary
if [ -f "docs/core/tasks.md" ]; then
    TOTAL=$(grep -c "^- \[" docs/core/tasks.md 2>/dev/null || echo "0")
    DONE=$(grep -c "^- \[x\]" docs/core/tasks.md 2>/dev/null || echo "0")
    ACTIVE=$(grep -c "^- \[ \]" docs/core/tasks.md 2>/dev/null || echo "0")
    
    PERCENT=0
    if [ $TOTAL -gt 0 ]; then
        PERCENT=$((DONE * 100 / TOTAL))
    fi
    
    echo -e "${BOLD}Tasks:${NC} $DONE/$TOTAL complete (${PERCENT}%) • $ACTIVE active"
fi

# Session Info
if [ -f "/tmp/current-session.txt" ]; then
    START_TIME=$(cat /tmp/current-session.txt)
    CURRENT_TIME=$(date +%s)
    DURATION=$((CURRENT_TIME - START_TIME))
    HOURS=$((DURATION / 3600))
    MINUTES=$(((DURATION % 3600) / 60))
    
    if [ $HOURS -ge 2 ]; then
        echo -e "${BOLD}Session:${NC} ${YELLOW}${HOURS}h ${MINUTES}m${NC} (consider wrapping up)"
    else
        echo -e "${BOLD}Session:${NC} ${GREEN}${HOURS}h ${MINUTES}m${NC}"
    fi
else
    echo -e "${BOLD}Session:${NC} Not tracked (run ./session-health-monitor.sh start)"
fi

# Rest Status
if [ -f "rest-tracker.txt" ]; then
    LAST_SESSION=$(grep "Session End:" rest-tracker.txt | tail -1 | cut -d' ' -f3-)
    LAST_EPOCH=$(date -d "$LAST_SESSION" +%s 2>/dev/null || echo "0")
    CURRENT_EPOCH=$(date +%s)
    REST_SECONDS=$((CURRENT_EPOCH - LAST_EPOCH))
    REST_HOURS=$((REST_SECONDS / 3600))
    
    if [ $REST_HOURS -ge 8 ]; then
        echo -e "${BOLD}Last Rest:${NC} ${GREEN}${REST_HOURS}h ago${NC} ✓"
    elif [ $REST_HOURS -ge 4 ]; then
        echo -e "${BOLD}Last Rest:${NC} ${GREEN}${REST_HOURS}h ago${NC}"
    else
        echo -e "${BOLD}Last Rest:${NC} ${YELLOW}${REST_HOURS}h ago${NC} (short)"
    fi
fi

echo ""

# Last Commit
if git rev-parse --git-dir > /dev/null 2>&1; then
    echo -e "${BOLD}Last Commit:${NC}"
    git log -1 --pretty=format:"  %h - %s (%cr)%n" --color=always
fi

echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e "${CYAN}Commands:${NC}"
echo "  ./pre-session-check.sh     - Detailed session start context"
echo "  ./session-health-monitor.sh check - Detailed session health"
echo "  ./status.sh                - This quick status (run anytime)"
echo ""
