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

###############################################################################
# setup-ai-log-monitor.sh
# Deployment script for WHP AI Log Monitor
# Idempotent — safe to run multiple times
###############################################################################

echo "=== WHP AI Log Monitor Setup ==="

# 1. Install Python 3 and pip if not present
if ! command -v python3 &>/dev/null || ! command -v pip3 &>/dev/null; then
    echo "Installing Python 3 and pip..."
    dnf install -y python3 python3-pip
else
    echo "Python 3 and pip already installed."
fi

# 2. Install the openai SDK
echo "Installing openai SDK..."
pip3 install --quiet openai

# 3. Create directories
echo "Creating directories..."
mkdir -p /var/lib/whp-ai-monitor/backups/
chmod 700 /var/lib/whp-ai-monitor/backups/
mkdir -p /etc/whp/

# 4. Copy default config if not exists
if [ ! -f /etc/whp/ai-monitor.conf ]; then
    if [ -f /root/whp/configs/ai-monitor.conf.default ]; then
        echo "Installing default config to /etc/whp/ai-monitor.conf..."
        cp /root/whp/configs/ai-monitor.conf.default /etc/whp/ai-monitor.conf
        chmod 600 /etc/whp/ai-monitor.conf
    else
        echo "WARNING: Default config not found at /root/whp/configs/ai-monitor.conf.default"
        echo "         Config will need to be installed manually."
    fi
else
    echo "Config /etc/whp/ai-monitor.conf already exists, skipping."
fi

# 5. Create API key placeholder if not exists
if [ ! -f /etc/whp/ai-monitor.key ]; then
    echo "Creating API key placeholder at /etc/whp/ai-monitor.key..."
    touch /etc/whp/ai-monitor.key
    chmod 600 /etc/whp/ai-monitor.key
else
    echo "API key file /etc/whp/ai-monitor.key already exists, skipping."
fi

# 6. Create log files
echo "Creating log files..."
touch /var/log/whp-ai-monitor.log
chmod 640 /var/log/whp-ai-monitor.log
touch /var/log/whp-ai-monitor-daily.log
chmod 640 /var/log/whp-ai-monitor-daily.log

# 7. Install logrotate config
echo "Installing logrotate config..."
cat > /etc/logrotate.d/whp-ai-monitor <<'LOGROTATE'
/var/log/whp-ai-monitor.log /var/log/whp-ai-monitor-daily.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    size 50M
}
LOGROTATE

# 8. Install cron jobs (idempotent — check before adding)
CRON_CRITICAL='*/5 * * * * /usr/bin/python3 /root/whp/scripts/whp-log-monitor-ai.py --mode critical >> /var/log/whp-ai-monitor.log 2>&1'
CRON_DAILY='0 6 * * * /usr/bin/python3 /root/whp/scripts/whp-log-monitor-ai.py --mode daily >> /var/log/whp-ai-monitor-daily.log 2>&1'

CURRENT_CRON=$(crontab -l 2>/dev/null || true)

if echo "$CURRENT_CRON" | grep -qF 'whp-log-monitor-ai.py --mode critical'; then
    echo "Critical mode cron job already exists, skipping."
else
    echo "Adding critical mode cron job (every 5 minutes)..."
    (echo "$CURRENT_CRON"; echo "$CRON_CRITICAL") | crontab -
    CURRENT_CRON=$(crontab -l 2>/dev/null || true)
fi

if echo "$CURRENT_CRON" | grep -qF 'whp-log-monitor-ai.py --mode daily'; then
    echo "Daily mode cron job already exists, skipping."
else
    echo "Adding daily mode cron job (6:00 AM)..."
    (echo "$CURRENT_CRON"; echo "$CRON_DAILY") | crontab -
fi

# 9. chmod +x the main script if it exists
if [ -f /root/whp/scripts/whp-log-monitor-ai.py ]; then
    echo "Setting executable permission on whp-log-monitor-ai.py..."
    chmod +x /root/whp/scripts/whp-log-monitor-ai.py
else
    echo "NOTE: /root/whp/scripts/whp-log-monitor-ai.py not found yet."
    echo "      It will be made executable once synced."
fi

# 10. Done
echo ""
echo "=== WHP AI Log Monitor Setup Complete ==="
echo ""
echo "Next steps:"
echo "  1. Set your API key:  echo 'your-api-key' > /etc/whp/ai-monitor.key"
echo "  2. Review config:     vi /etc/whp/ai-monitor.conf"
echo "  3. Test with dry run: python3 /root/whp/scripts/whp-log-monitor-ai.py --dry-run"
