#!/bin/bash

# ═══════════════════════════════════════════════════════════════════════════
# PRE-SESSION-CHECK.SH - Session Start Context Dashboard
# ═══════════════════════════════════════════════════════════════════════════
# 
# Purpose: Show complete context at session start
# Created: February 20, 2026 by Chronicler #19
# 
# Usage: ./pre-session-check.sh
# 
# ═══════════════════════════════════════════════════════════════════════════

set -e

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

print_header() {
    echo -e "${BLUE}═══════════════════════════════════════════════════════════════════════════${NC}"
    echo -e "${CYAN}${BOLD}$1${NC}"
    echo -e "${BLUE}═══════════════════════════════════════════════════════════════════════════${NC}"
    echo ""
}

print_section() {
    echo ""
    echo -e "${CYAN}${BOLD}━━━ $1 ━━━${NC}"
    echo ""
}

print_success() {
    echo -e "${GREEN}✓${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

print_error() {
    echo -e "${RED}✗${NC} $1"
}

clear

print_header "FIREFROST GAMING - SESSION START CONTEXT"

# ═══════════════════════════════════════════════════════════════════════════
# REST TRACKING
# ═══════════════════════════════════════════════════════════════════════════

print_section "Rest Since Last Session"

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))
    REST_MINUTES=$(((REST_SECONDS % 3600) / 60))
    
    if [ $REST_HOURS -ge 8 ]; then
        print_success "Rest: ${REST_HOURS}h ${REST_MINUTES}m (Excellent - Aurora approved)"
    elif [ $REST_HOURS -ge 4 ]; then
        echo -e "${GREEN}✓${NC} Rest: ${REST_HOURS}h ${REST_MINUTES}m (Good)"
    elif [ $REST_HOURS -ge 2 ]; then
        print_warning "Rest: ${REST_HOURS}h ${REST_MINUTES}m (Adequate)"
    else
        print_error "Rest: ${REST_HOURS}h ${REST_MINUTES}m (Too short - consider resting more)"
    fi
    
    echo "   Last session ended: $(date -d "$LAST_SESSION" '+%Y-%m-%d %I:%M %p %Z' 2>/dev/null || echo "$LAST_SESSION")"
else
    print_warning "No rest-tracker.txt found (first session or file missing)"
fi

# ═══════════════════════════════════════════════════════════════════════════
# GIT STATUS
# ═══════════════════════════════════════════════════════════════════════════

print_section "Repository Status"

# Check if we're in a git repo
if git rev-parse --git-dir > /dev/null 2>&1; then
    # Current branch
    BRANCH=$(git branch --show-current)
    echo -e "Branch: ${BOLD}$BRANCH${NC}"
    
    # Check for uncommitted changes
    if git diff-index --quiet HEAD -- 2>/dev/null; then
        print_success "Working directory clean"
    else
        print_warning "Uncommitted changes present"
        echo ""
        git status --short
    fi
    
    echo ""
    
    # Check remote status
    git fetch origin --quiet 2>/dev/null || true
    AHEAD=$(git rev-list --count origin/$BRANCH..$BRANCH 2>/dev/null || echo "0")
    BEHIND=$(git rev-list --count $BRANCH..origin/$BRANCH 2>/dev/null || echo "0")
    
    if [ "$AHEAD" -gt 0 ]; then
        print_warning "Ahead of origin by $AHEAD commit(s) - need to push"
    fi
    
    if [ "$BEHIND" -gt 0 ]; then
        print_warning "Behind origin by $BEHIND commit(s) - need to pull"
    fi
    
    if [ "$AHEAD" -eq 0 ] && [ "$BEHIND" -eq 0 ]; then
        print_success "In sync with origin"
    fi
    
    echo ""
    
    # Last commit
    echo -e "${BOLD}Last Commit:${NC}"
    git log -1 --pretty=format:"   %h - %s%n   %cr by %an%n" --color=always
    
else
    print_error "Not in a git repository"
fi

# ═══════════════════════════════════════════════════════════════════════════
# CURRENT PHASE & TASKS
# ═══════════════════════════════════════════════════════════════════════════

print_section "Current Work Status"

if [ -f "docs/core/tasks.md" ]; then
    # Count tasks by status
    TOTAL_TASKS=$(grep -c "^- \[" docs/core/tasks.md 2>/dev/null || echo "0")
    COMPLETE_TASKS=$(grep -c "^- \[x\]" docs/core/tasks.md 2>/dev/null || echo "0")
    ACTIVE_TASKS=$(grep -c "^- \[ \]" docs/core/tasks.md 2>/dev/null || echo "0")
    
    echo "Tasks: $COMPLETE_TASKS complete, $ACTIVE_TASKS active, $TOTAL_TASKS total"
    echo ""
    
    # Show top 3 active tasks
    echo -e "${BOLD}Top Priority Tasks:${NC}"
    TASK_COUNT=$(grep -c "^- \[ \]" docs/core/tasks.md 2>/dev/null || echo "0")
    if [ "$TASK_COUNT" -gt 0 ]; then
        grep "^- \[ \]" docs/core/tasks.md | head -3 | sed 's/^- \[ \] /   • /'
        
        if [ "$TASK_COUNT" -gt 3 ]; then
            echo "   ... and $((TASK_COUNT - 3)) more"
        fi
    else
        echo "   (No active tasks)"
    fi
else
    print_warning "tasks.md not found"
fi

# ═══════════════════════════════════════════════════════════════════════════
# SYSTEM HEALTH (if available)
# ═══════════════════════════════════════════════════════════════════════════

print_section "Infrastructure Quick Check"

# Check if key files exist
CHECKS=0
PASSED=0

if [ -f "docs/core/infrastructure-manifest.md" ]; then
    print_success "Infrastructure manifest present"
    ((PASSED++))
fi
((CHECKS++))

if [ -f "SESSION-HANDOFF-PROTOCOL.md" ]; then
    print_success "Session handoff protocol present"
    ((PASSED++))
fi
((CHECKS++))

if [ -f "docs/relationship/THE-ESSENCE-PATCH-V3.0.md" ]; then
    print_success "Essence Patch v3.0 present"
    ((PASSED++))
fi
((CHECKS++))

if [ -f "automation/joining-checklist.py" ]; then
    print_success "Joining verification available"
    ((PASSED++))
fi
((CHECKS++))

echo ""
echo "Core files: $PASSED/$CHECKS present"

# ═══════════════════════════════════════════════════════════════════════════
# READY TO START
# ═══════════════════════════════════════════════════════════════════════════

echo ""
print_header "SESSION READY"

echo -e "${CYAN}Repository:${NC} $(pwd)"
echo -e "${CYAN}Documentation:${NC} docs/"
echo -e "${CYAN}Tasks:${NC} docs/core/tasks.md"
echo ""

if [ $REST_HOURS -ge 8 ]; then
    echo -e "${GREEN}${BOLD}Excellent rest. Ready for productive work.${NC}"
elif [ $REST_HOURS -ge 4 ]; then
    echo -e "${GREEN}Good rest. Ready to work.${NC}"
elif [ $REST_HOURS -ge 2 ]; then
    echo -e "${YELLOW}Adequate rest. Monitor session length.${NC}"
else
    echo -e "${RED}${BOLD}Short rest. Consider brief session or more rest first.${NC}"
fi

echo ""
echo -e "${CYAN}Fire + Frost + Foundation = Where Love Builds Legacy${NC}"
echo ""
echo -e "💙🔥❄️"
echo ""
