#!/bin/bash
# =============================================================================
# 🚀 POUNCE AUTOMATED DATA COLLECTION - CRON SETUP
# =============================================================================
#
# This script sets up automated data collection for premium service.
#
# Schedule:
#   - TLD Prices:    Every 6 hours (0:00, 6:00, 12:00, 18:00)
#   - Auctions:      Every 2 hours
#   - Quality Report: Daily at 1:00 AM
#
# Usage:
#   ./setup_cron.sh           # Install cron jobs
#   ./setup_cron.sh --remove  # Remove cron jobs
#   ./setup_cron.sh --status  # Show current cron jobs
#
# =============================================================================

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
PYTHON_PATH="${PROJECT_DIR}/.venv/bin/python"
COLLECTOR_SCRIPT="${SCRIPT_DIR}/premium_data_collector.py"
LOG_DIR="${PROJECT_DIR}/logs"

# Ensure log directory exists
mkdir -p "$LOG_DIR"

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

print_status() {
    echo -e "${GREEN}[✓]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[!]${NC} $1"
}

print_error() {
    echo -e "${RED}[✗]${NC} $1"
}

# Cron job definitions
CRON_MARKER="# POUNCE_DATA_COLLECTOR"

TLD_CRON="0 */6 * * * cd ${PROJECT_DIR} && ${PYTHON_PATH} ${COLLECTOR_SCRIPT} --tld --quiet >> ${LOG_DIR}/tld_collection.log 2>&1 ${CRON_MARKER}"
AUCTION_CRON="0 */2 * * * cd ${PROJECT_DIR} && ${PYTHON_PATH} ${COLLECTOR_SCRIPT} --auctions --quiet >> ${LOG_DIR}/auction_collection.log 2>&1 ${CRON_MARKER}"
REPORT_CRON="0 1 * * * cd ${PROJECT_DIR} && ${PYTHON_PATH} ${COLLECTOR_SCRIPT} --report --quiet >> ${LOG_DIR}/quality_report.log 2>&1 ${CRON_MARKER}"

install_cron() {
    echo "🚀 Installing Pounce Data Collection Cron Jobs..."
    echo ""
    
    # Check if Python environment exists
    if [ ! -f "$PYTHON_PATH" ]; then
        print_error "Python virtual environment not found at: $PYTHON_PATH"
        echo "Please create it first: python -m venv .venv && .venv/bin/pip install -r requirements.txt"
        exit 1
    fi
    
    # Check if collector script exists
    if [ ! -f "$COLLECTOR_SCRIPT" ]; then
        print_error "Collector script not found at: $COLLECTOR_SCRIPT"
        exit 1
    fi
    
    # Remove existing Pounce cron jobs first
    (crontab -l 2>/dev/null | grep -v "$CRON_MARKER") | crontab -
    
    # Add new cron jobs
    (crontab -l 2>/dev/null; echo "$TLD_CRON") | crontab -
    (crontab -l 2>/dev/null; echo "$AUCTION_CRON") | crontab -
    (crontab -l 2>/dev/null; echo "$REPORT_CRON") | crontab -
    
    print_status "TLD Price Collection: Every 6 hours"
    print_status "Auction Collection: Every 2 hours"
    print_status "Quality Report: Daily at 1:00 AM"
    echo ""
    print_status "All cron jobs installed successfully!"
    echo ""
    echo "Log files will be written to: ${LOG_DIR}/"
    echo ""
    echo "To view current jobs: crontab -l"
    echo "To remove jobs: $0 --remove"
}

remove_cron() {
    echo "🗑️  Removing Pounce Data Collection Cron Jobs..."
    
    (crontab -l 2>/dev/null | grep -v "$CRON_MARKER") | crontab -
    
    print_status "All Pounce cron jobs removed."
}

show_status() {
    echo "📋 Current Pounce Cron Jobs:"
    echo ""
    
    JOBS=$(crontab -l 2>/dev/null | grep "$CRON_MARKER" || true)
    
    if [ -z "$JOBS" ]; then
        print_warning "No Pounce cron jobs found."
        echo ""
        echo "Run '$0' to install them."
    else
        echo "$JOBS" | while read -r line; do
            echo "  $line"
        done
        echo ""
        print_status "Jobs are active."
    fi
}

# Main
case "${1:-}" in
    --remove)
        remove_cron
        ;;
    --status)
        show_status
        ;;
    *)
        install_cron
        ;;
esac

