#!/bin/bash

# Setup Backup Cron Job
# Glado POS System

echo "=== Glado POS - Backup Cron Setup ==="
echo

# Get the current directory (where the script is located)
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

# Check if PHP is available
if ! command -v php &> /dev/null; then
    echo "Error: PHP is not installed or not in PATH"
    exit 1
fi

echo "✓ PHP found: $(php --version | head -n1)"

# Check if the backup cron script exists
CRON_SCRIPT="$SCRIPT_DIR/backup_cron.php"
if [ ! -f "$CRON_SCRIPT" ]; then
    echo "Error: Backup cron script not found at $CRON_SCRIPT"
    exit 1
fi

echo "✓ Backup cron script found"

# Make sure the script is executable
chmod +x "$CRON_SCRIPT"

# Create logs directory if it doesn't exist
LOGS_DIR="$PROJECT_DIR/logs"
if [ ! -d "$LOGS_DIR" ]; then
    mkdir -p "$LOGS_DIR"
    echo "✓ Created logs directory"
else
    echo "✓ Logs directory exists"
fi

# Set proper permissions
chmod 755 "$LOGS_DIR"

# Create backups directory if it doesn't exist
BACKUPS_DIR="$PROJECT_DIR/backups"
if [ ! -d "$BACKUPS_DIR" ]; then
    mkdir -p "$BACKUPS_DIR"
    echo "✓ Created backups directory"
else
    echo "✓ Backups directory exists"
fi

# Set proper permissions for backups directory
chmod 755 "$BACKUPS_DIR"

# Test the backup script
echo
echo "Testing backup cron script..."
php "$CRON_SCRIPT" --test 2>&1

if [ $? -eq 0 ]; then
    echo "✓ Backup script test passed"
else
    echo "⚠ Backup script test had issues (check logs for details)"
fi

# Generate cron job entry
CRON_ENTRY="0 23 * * * /usr/bin/php $CRON_SCRIPT >> $LOGS_DIR/backup_cron.log 2>&1"

echo
echo "=== Cron Job Setup ==="
echo
echo "To set up automatic daily backups, add the following line to your crontab:"
echo
echo "$CRON_ENTRY"
echo
echo "To edit your crontab, run:"
echo "crontab -e"
echo
echo "This will run backups daily at 11:59 PM."
echo

# Ask if user wants to add the cron job automatically
read -p "Would you like to add this cron job automatically? (y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    # Check if cron job already exists
    if crontab -l 2>/dev/null | grep -q "$CRON_SCRIPT"; then
        echo "⚠ Cron job already exists for this script"
    else
        # Add the cron job
        (crontab -l 2>/dev/null; echo "$CRON_ENTRY") | crontab -
        if [ $? -eq 0 ]; then
            echo "✓ Cron job added successfully"
        else
            echo "✗ Failed to add cron job"
        fi
    fi
else
    echo "Cron job not added. You can add it manually later."
fi

echo
echo "=== Additional Setup Notes ==="
echo
echo "1. Make sure your database credentials are properly configured"
echo "2. For Google Drive sync, configure the following environment variables:"
echo "   - GOOGLE_CLIENT_ID"
echo "   - GOOGLE_CLIENT_SECRET"
echo "   - GOOGLE_REFRESH_TOKEN"
echo "   - GOOGLE_DRIVE_FOLDER_ID"
echo
echo "3. Backup settings can be configured per branch in the admin panel"
echo "4. Log files will be stored in: $LOGS_DIR"
echo "5. Backup files will be stored in: $BACKUPS_DIR"
echo
echo "Setup complete!"