#!/bin/bash

# ChatBud PHP - Quick Setup Script
# This script helps set up ChatBud PHP backend quickly

echo "=================================================="
echo "        ChatBud PHP - Setup Script"
echo "=================================================="
echo ""

# Check PHP version
echo "1. Checking PHP version..."
php_version=$(php -v | head -1)
echo "   ✓ $php_version"
echo ""

# Check MySQL/PostgreSQL
echo "2. Checking database availability..."
if command -v mysql &> /dev/null; then
    echo "   ✓ MySQL found"
fi
if command -v psql &> /dev/null; then
    echo "   ✓ PostgreSQL found"
fi
echo ""

# Create .env file
echo "3. Creating environment configuration..."
if [ ! -f .env ]; then
    cp .env.example .env
    echo "   ✓ .env file created from .env.example"
else
    echo "   ✓ .env file already exists"
fi
echo ""

# Create necessary directories
echo "4. Creating required directories..."
mkdir -p public/uploads/posts
mkdir -p public/uploads/avatars
mkdir -p public/uploads/temp
mkdir -p src/templates/emails
echo "   ✓ Directories created"
echo ""

# Set file permissions
echo "5. Setting file permissions..."
chmod 755 public/
chmod 755 public/uploads/
chmod 644 public/.htaccess 2>/dev/null || true
chmod 755 src/
echo "   ✓ Permissions set"
echo ""

# Check database connection
echo "6. Testing database connection..."
php test_db.php 2>/dev/null || echo "   ⚠ Database connection test skipped (test_db.php not found)"
echo ""

# Composer install
echo "7. Installing PHP dependencies (optional)..."
if [ -f composer.json ]; then
    if command -v composer &> /dev/null; then
        composer install
        echo "   ✓ Composer dependencies installed"
    else
        echo "   ⚠ Composer not found. Run: composer install"
    fi
else
    echo "   ⚠ No composer.json found (optional for this project)"
fi
echo ""

echo "=================================================="
echo "        Setup Complete! 🎉"
echo "=================================================="
echo ""
echo "Next steps:"
echo "1. Edit .env with your database credentials"
echo "2. Import database.sql into your database"
echo "3. Edit .env with your email configuration"
echo "4. Start your web server"
echo "5. Visit: http://localhost/chatbud-php/public/"
echo ""
echo "Documentation:"
echo "- API: See API_DOCUMENTATION.md"
echo "- Database: See DATABASE_SETUP.md"
echo "- Email: See EMAIL_SETUP.md"
echo "- Full: See README_COMPLETE.md"
echo ""
echo "Support: support@chatbud.com"
echo ""
