#!/bin/bash
# Setup script for Mawidi SEO Daily Agent
# Run this once to configure automatic 7:00 AM scheduling

set -e

PROJECT_DIR="/Users/Asim/Desktop/mawidi_codex"
SCRIPTS_DIR="$PROJECT_DIR/scripts"
REPORTS_DIR="$PROJECT_DIR/seo-reports"
PLIST_FILE="$SCRIPTS_DIR/com.mawidi.seo-agent.plist"

echo "=========================================="
echo "Mawidi SEO Agent Setup"
echo "=========================================="

# Create directories
mkdir -p "$REPORTS_DIR"
echo "[OK] Reports directory created: $REPORTS_DIR"

# Make script executable
chmod +x "$SCRIPTS_DIR/seo-daily-agent.sh"
echo "[OK] Made seo-daily-agent.sh executable"

# Choose installation method
echo ""
echo "Choose scheduling method:"
echo "1) macOS launchd (recommended for Mac)"
echo "2) cron job (cross-platform)"
echo "3) Skip (manual setup later)"
read -p "Enter choice [1-3]: " choice

case $choice in
    1)
        echo "Installing launchd agent..."

        # Copy plist to LaunchAgents
        cp "$PLIST_FILE" ~/Library/LaunchAgents/

        # Load the agent
        launchctl load ~/Library/LaunchAgents/com.mawidi.seo-agent.plist

        echo "[OK] launchd agent installed and loaded"
        echo "    Agent will run daily at 7:00 AM"
        echo ""
        echo "Management commands:"
        echo "  - Check status: launchctl list | grep mawidi"
        echo "  - Unload:       launchctl unload ~/Library/LaunchAgents/com.mawidi.seo-agent.plist"
        echo "  - Run now:      launchctl start com.mawidi.seo-agent"
        ;;
    2)
        echo "Setting up cron job..."

        # Add cron job
        CRON_CMD="0 7 * * * $SCRIPTS_DIR/seo-daily-agent.sh >> $REPORTS_DIR/cron.log 2>&1"

        # Check if already exists
        if crontab -l 2>/dev/null | grep -q "seo-daily-agent"; then
            echo "[INFO] Cron job already exists. Updating..."
            crontab -l 2>/dev/null | grep -v "seo-daily-agent" | crontab -
        fi

        # Add new cron job
        (crontab -l 2>/dev/null; echo "$CRON_CMD") | crontab -

        echo "[OK] Cron job installed"
        echo "    Will run daily at 7:00 AM"
        echo ""
        echo "Management commands:"
        echo "  - View:   crontab -l"
        echo "  - Edit:   crontab -e"
        echo "  - Remove: crontab -l | grep -v 'seo-daily-agent' | crontab -"
        ;;
    3)
        echo "Skipping automatic setup."
        echo ""
        echo "Manual setup instructions:"
        echo ""
        echo "Option A - launchd (macOS):"
        echo "  cp $PLIST_FILE ~/Library/LaunchAgents/"
        echo "  launchctl load ~/Library/LaunchAgents/com.mawidi.seo-agent.plist"
        echo ""
        echo "Option B - cron:"
        echo "  crontab -e"
        echo "  Add: 0 7 * * * $SCRIPTS_DIR/seo-daily-agent.sh"
        ;;
    *)
        echo "Invalid choice. Exiting."
        exit 1
        ;;
esac

echo ""
echo "=========================================="
echo "Setup Complete!"
echo ""
echo "Test the agent manually:"
echo "  $SCRIPTS_DIR/seo-daily-agent.sh"
echo ""
echo "Or use Claude Code:"
echo "  /seo-monitor full"
echo "=========================================="
