#!/usr/bin/env bash
set -euo pipefail

###############################################################################
# setup-customer-ai-monitor.sh
# Sets up customer AI monitoring cron jobs, dependencies, and configuration.
# Idempotent - safe to run multiple times.
###############################################################################

echo "=== Setting up Customer AI Monitor ==="

# ---------------------------------------------------------------------------
# 1. Install pymysql Python package if not already installed
# ---------------------------------------------------------------------------
echo "Checking Python dependencies..."
if ! python3 -c "import pymysql" 2>/dev/null; then
    echo "Installing pymysql..."
    pip3 install pymysql
else
    echo "pymysql already installed"
fi

# ---------------------------------------------------------------------------
# 2. Set up cron jobs (idempotent)
# ---------------------------------------------------------------------------
add_cron_job() {
    local job="$1"
    local comment="$2"
    if ! crontab -l 2>/dev/null | grep -qF "$job"; then
        (crontab -l 2>/dev/null; echo "# $comment"; echo "$job") | crontab -
        echo "Added cron job: $comment"
    else
        echo "Cron job already exists: $comment"
    fi
}

echo "Configuring cron jobs..."

add_cron_job \
    "* * * * * /usr/bin/php /root/whp/scripts/process-email-queue.php >> /var/log/whp-email-queue.log 2>&1" \
    "Process AI monitor email queue (every minute)"

add_cron_job \
    "* * * * * /usr/bin/php /root/whp/scripts/process-log-review.php >> /var/log/whp-log-review.log 2>&1" \
    "Process AI monitor log reviews (every minute)"

add_cron_job \
    "0 3 * * * /usr/bin/php /root/whp/scripts/ai-monitor-maintenance.php >> /var/log/whp-ai-monitor-maintenance.log 2>&1" \
    "AI monitor data maintenance (daily at 3 AM)"

# ---------------------------------------------------------------------------
# 3. Create log files with proper permissions
# ---------------------------------------------------------------------------
echo "Setting up log files..."
touch /var/log/whp-email-queue.log
touch /var/log/whp-log-review.log
touch /var/log/whp-ai-monitor-maintenance.log
chmod 640 /var/log/whp-email-queue.log /var/log/whp-log-review.log /var/log/whp-ai-monitor-maintenance.log

# ---------------------------------------------------------------------------
# 4. Add logrotate configuration
# ---------------------------------------------------------------------------
echo "Configuring logrotate..."
cat > /etc/logrotate.d/whp-ai-monitor-customer <<'LOGROTATE'
/var/log/whp-email-queue.log /var/log/whp-log-review.log /var/log/whp-ai-monitor-maintenance.log {
    weekly
    rotate 4
    compress
    missingok
    notifempty
}
LOGROTATE
echo "Logrotate configuration written to /etc/logrotate.d/whp-ai-monitor-customer"

# ---------------------------------------------------------------------------
# 5. Initialize customer_monitoring.enabled in settings.json if not present
# ---------------------------------------------------------------------------
echo "Checking settings.json..."
if [ -f /docker/whp/settings.json ]; then
    python3 -c "
import json
with open('/docker/whp/settings.json', 'r') as f:
    cfg = json.load(f)
if 'customer_monitoring' not in cfg:
    cfg['customer_monitoring'] = {'enabled': True}
    with open('/docker/whp/settings.json', 'w') as f:
        json.dump(cfg, f, indent=4)
    print('Added customer_monitoring to settings.json')
else:
    print('customer_monitoring already configured')
"
else
    echo "settings.json not found at /docker/whp/settings.json - skipping configuration"
fi

# ---------------------------------------------------------------------------
# 6. Success
# ---------------------------------------------------------------------------
echo ""
echo "=== Customer AI Monitor setup complete ==="
