#!/bin/bash
# ============================================
# Claude Code Project Startup Script
# Automatically runs when project is opened
# ============================================

PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
STARTUP_LOG="/tmp/claude-startup.log"

# Function to log
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$STARTUP_LOG"
}

# Start logging
log "Starting Claude Code project: $PROJECT_DIR"

# Check if Docker is running
if command -v docker &> /dev/null && docker info &> /dev/null; then
    log "Docker is running"

    # Check if auto-update should be enabled
    if [ -f "/tmp/docker-auto-memory" ]; then
        source /tmp/docker-auto-memory
        if [ "$AUTO_UPDATE_ACTIVE" = "true" ]; then
            log "Restoring Docker auto-update state..."
            bash "$PROJECT_DIR/.claude/scripts/docker-auto-daemon.sh" start &> /dev/null
            echo "✅ Docker auto-update restored and running"
        fi
    else
        # First time setup - ask user
        echo "🐳 Docker detected. Would you like to enable auto-updates?"
        echo "   This will automatically update containers when you change files."
        echo "   You can manage this later with: /docker-auto"

        # Default to enabled for convenience
        log "Enabling Docker auto-update by default"
        bash "$PROJECT_DIR/.claude/scripts/docker-auto-daemon.sh" start &> /dev/null
        echo "AUTO_UPDATE_ACTIVE=true" > /tmp/docker-auto-memory
        echo "✅ Docker auto-update enabled (use /docker-auto stop to disable)"
    fi
else
    log "Docker not running or not installed"
fi

# Display welcome message
cat << EOF

╔════════════════════════════════════════════╗
║     Mawidi Codex - Development Ready       ║
╠════════════════════════════════════════════╣
║ 🚀 Quick Commands:                         ║
║   make dev        - Start with hot-reload  ║
║   make dev-watch  - Auto-rebuild mode      ║
║   /docker-auto    - Manage auto-updates    ║
║                                            ║
║ 📁 Project: $PROJECT_DIR
║ 📝 Logs: $STARTUP_LOG
╚════════════════════════════════════════════╝

EOF

log "Startup complete"