#!/bin/bash
# Expenses Management System - Production Deployment Script
# HAVEN GRAZURI Advance

set -e  # Exit on error

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Functions
print_header() {
    echo -e "\n${BLUE}========================================${NC}"
    echo -e "${BLUE}$1${NC}"
    echo -e "${BLUE}========================================${NC}\n"
}

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

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

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

print_info() {
    echo -e "${BLUE}ℹ $1${NC}"
}

# Start deployment
print_header "EXPENSES SYSTEM DEPLOYMENT"
echo "Started at: $(date)"

# Step 1: Check Python
print_info "Checking Python installation..."
if command -v python &> /dev/null; then
    PYTHON_CMD=python
elif command -v python3 &> /dev/null; then
    PYTHON_CMD=python3
else
    print_error "Python not found!"
    exit 1
fi
print_success "Python found: $($PYTHON_CMD --version)"

# Step 2: Check if manage.py exists
print_info "Checking Django project..."
if [ ! -f "manage.py" ]; then
    print_error "manage.py not found! Are you in the project root?"
    exit 1
fi
print_success "Django project found"

# Step 3: Install/Update dependencies
print_header "CHECKING DEPENDENCIES"
if [ -f "requirements.txt" ]; then
    print_info "Installing/updating dependencies..."
    $PYTHON_CMD -m pip install -r requirements.txt --quiet
    print_success "Dependencies installed"
else
    print_warning "requirements.txt not found, skipping dependency installation"
fi

# Step 4: Run migrations
print_header "RUNNING MIGRATIONS"
print_info "Creating migrations for expenses app..."
$PYTHON_CMD manage.py makemigrations expenses || print_warning "No new migrations to create"

print_info "Applying all migrations..."
$PYTHON_CMD manage.py migrate
print_success "Migrations completed"

# Step 5: Collect static files (production only)
if [ "$1" == "--production" ]; then
    print_header "COLLECTING STATIC FILES"
    print_info "Collecting static files..."
    $PYTHON_CMD manage.py collectstatic --noinput
    print_success "Static files collected"
fi

# Step 6: Create sample data (optional)
if [ "$1" != "--skip-sample-data" ]; then
    print_header "SAMPLE DATA"
    read -p "Do you want to create sample expense data? (y/n): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        print_info "Creating sample data..."
        $PYTHON_CMD create_sample_expenses.py
        print_success "Sample data created"
    else
        print_info "Skipping sample data creation"
    fi
fi

# Step 7: Run system check
print_header "SYSTEM CHECK"
print_info "Running Django system check..."
$PYTHON_CMD manage.py check
print_success "System check passed"

# Step 8: Verify installation
print_header "VERIFICATION"
print_info "Verifying expenses installation..."
$PYTHON_CMD verify_expenses.py
print_success "Verification completed"

# Step 9: Clear cache (if applicable)
print_header "CACHE MANAGEMENT"
if [ -f "clear_django_cache.py" ]; then
    print_info "Clearing Django cache..."
    $PYTHON_CMD clear_django_cache.py || print_warning "Cache clearing skipped"
else
    print_info "No cache clearing script found, skipping"
fi

# Deployment summary
print_header "DEPLOYMENT COMPLETE"
print_success "Expenses Management System deployed successfully!"

echo -e "\n${GREEN}Next Steps:${NC}"
echo "1. Start the server: python manage.py runserver"
echo "2. Navigate to: http://localhost:8000/expenses/"
echo "3. Login and test the features"

echo -e "\n${BLUE}Available URLs:${NC}"
echo "  • Expenses List:      /expenses/"
echo "  • Add Expense:        /expenses/add/"
echo "  • Pending Approvals:  /expenses/approvals/pending/"
echo "  • Analytics:          /expenses/analytics/"

echo -e "\n${BLUE}Documentation:${NC}"
echo "  • User Guide:         EXPENSES_USER_GUIDE.md"
echo "  • Technical Docs:     EXPENSES_FEATURE_DOCUMENTATION.md"
echo "  • Quick Start:        EXPENSES_README.md"

echo -e "\n${GREEN}Deployment completed at: $(date)${NC}\n"
