#!/bin/bash
# SEO Daily Intelligence Agent Launcher
# Runs at 7:00 AM daily to monitor SEO trends and implement improvements
#
# Setup cron job:
#   crontab -e
#   0 7 * * * /Users/Asim/Desktop/mawidi_codex/scripts/seo-daily-agent.sh >> /Users/Asim/Desktop/mawidi_codex/seo-reports/cron.log 2>&1
#
# Or use launchd for macOS (preferred):
#   See: scripts/com.mawidi.seo-agent.plist

set -e

# Configuration
PROJECT_DIR="/Users/Asim/Desktop/mawidi_codex"
REPORTS_DIR="$PROJECT_DIR/seo-reports"
LOG_FILE="$REPORTS_DIR/agent-$(date +%Y-%m-%d).log"
REPORT_FILE="$REPORTS_DIR/$(date +%Y-%m-%d)-seo-report.md"

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

echo "=========================================="
echo "Mawidi SEO Daily Intelligence Agent"
echo "Date: $(date '+%Y-%m-%d %H:%M:%S')"
echo "=========================================="

# Ensure reports directory exists
mkdir -p "$REPORTS_DIR"

# Log start
echo "[$(date '+%H:%M:%S')] Starting SEO monitoring agent..." | tee -a "$LOG_FILE"

# Check if Claude CLI is available
if ! command -v claude &> /dev/null; then
    echo -e "${RED}[ERROR] Claude CLI not found. Please install Claude Code.${NC}" | tee -a "$LOG_FILE"
    exit 1
fi

# Navigate to project directory
cd "$PROJECT_DIR"

# Run the SEO monitor command with Claude
echo "[$(date '+%H:%M:%S')] Launching Claude with /seo-monitor command..." | tee -a "$LOG_FILE"

# Option 1: Interactive mode with the slash command
# claude "/seo-monitor full"

# Option 2: Headless mode with prompt
claude --print "Execute the daily SEO monitoring routine:

1. **Research Current Trends** (use WebSearch):
   - Search for 'Google algorithm update December 2025'
   - Search for 'ChatGPT SEO optimization 2025'
   - Search for 'healthcare SEO trends 2025'
   - Search for 'Arabic SEO GCC market 2025'

2. **Audit Site** (read key files):
   - Check app/[lang]/layout.tsx for metadata
   - Check components/JsonLd.tsx for schema
   - Review app/sitemap.ts for coverage

3. **Generate Report**:
   Save a comprehensive report to: $REPORT_FILE

4. **Implement Quick Fixes**:
   - Fix any critical SEO issues found
   - Update stale metadata if needed
   - Enhance schema markup if gaps found

Focus on: AI search optimization, GCC healthcare market, Arabic SEO.
Report any issues that need manual attention." 2>&1 | tee -a "$LOG_FILE"

# Check if report was generated
if [ -f "$REPORT_FILE" ]; then
    echo -e "${GREEN}[$(date '+%H:%M:%S')] Report generated successfully: $REPORT_FILE${NC}" | tee -a "$LOG_FILE"
else
    echo -e "${YELLOW}[$(date '+%H:%M:%S')] Report file not found. Check agent output.${NC}" | tee -a "$LOG_FILE"
fi

# Summary
echo ""
echo "=========================================="
echo "SEO Agent Complete"
echo "Log: $LOG_FILE"
echo "Report: $REPORT_FILE"
echo "=========================================="

# Optional: Send notification
# osascript -e 'display notification "SEO Daily Report Ready" with title "Mawidi SEO Agent"'

exit 0
