#!/bin/bash
# Setup local development environment

set -e

echo "🛠️  Setting up XAMXAM development environment"
echo "=============================================="
echo ""

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# Create vendor directory
if [ ! -d "vendor" ]; then
    mkdir -p vendor
    echo "✓ Created vendor/ directory"
fi

# Clone php-live-reload
if [ -d "vendor/php-live-reload" ]; then
    echo -e "${YELLOW}⚠️  php-live-reload already exists${NC}"
    read -p "Update it? [y/N] " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        rm -rf vendor/php-live-reload
        git clone --depth 1 https://github.com/ryantate13/php-live-reload.git vendor/php-live-reload
        echo "✓ Updated php-live-reload"
    fi
else
    echo "📥 Cloning php-live-reload..."
    git clone --depth 1 https://github.com/ryantate13/php-live-reload.git vendor/php-live-reload
    echo "✓ Cloned php-live-reload"
fi

# Create xamxam.db if needed
if [ ! -f "storage/xamxam.db" ]; then
    echo ""
    echo "📊 Creating xamxam.db…"
    sqlite3 storage/xamxam.db < storage/schema.sql
    echo "✓ Created xamxam.db"
fi

# Create data directories
echo ""
echo "📁 Creating data directories..."
mkdir -p admin/data/theses
mkdir -p admin/data/covers
mkdir -p admin/data/yaml
touch admin/data/theses/.gitkeep
touch admin/data/covers/.gitkeep
echo "✓ Created data directories"

echo ""
echo "═══════════════════════════════════════"
echo -e "${GREEN}✅ Development environment ready!${NC}"
echo "═══════════════════════════════════════"
echo ""
echo "🚀 Start developing:"
echo "  just serve-public  # Start public site with live reload"
echo "  just serve-admin   # Start admin panel with live reload"
echo ""
echo "📝 The browser will auto-refresh when you save PHP files!"
echo ""
