#!/bin/bash
# =============================================================================
# FLHIP Weekly Master Pipeline
# =============================================================================
# Updated: 2026-01-10
#
# LOCATION: ~/flhip/weekly_flhip_master.sh
#
# CRON SCHEDULE (Friday 3:00 AM):
#   0 3 * * 5 /home/dh_q8pqvp/data.flhip.com/weekly_flhip_master.sh >> /home/dh_q8pqvp/data.flhip.com/logs/weekly_master_cron.log 2>&1
#
# This master script runs:
#   1. Restaurant scraper (waits for completion)
#   2. Hotel scraper (waits for completion)
#   3. Combined email report with both CSVs attached
#
# ESTIMATED COSTS PER WEEK:
#   Restaurant: ~$4 articles + ~$27.50 contacts = ~$31.50
#   Hotel:      ~$1 articles + ~$7.50 contacts  = ~$8.50
#   TOTAL:      ~$40/week
# =============================================================================

# Exit on error
set -e

# Base directory
FLHIP_DIR="/home/dh_q8pqvp/data.flhip.com"

# Logging
LOG_DIR="$FLHIP_DIR/logs"
mkdir -p "$LOG_DIR"
MASTER_LOG="$LOG_DIR/weekly_master_$(date +%Y%m%d_%H%M%S).log"

# Function to log messages
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$MASTER_LOG"
}

log "============================================================"
log "FLHIP WEEKLY MASTER PIPELINE"
log "Started: $(date)"
log "============================================================"

# -----------------------------------------------------------------------------
# STEP 1: Restaurant Scraper
# -----------------------------------------------------------------------------
log ""
log "[MASTER STEP 1] Running Restaurant Scraper..."
log "============================================================"

if [ -f "$FLHIP_DIR/weekly_flhip_scraper.sh" ]; then
    bash "$FLHIP_DIR/weekly_flhip_scraper.sh" 2>&1 | tee -a "$MASTER_LOG"
    RESTAURANT_STATUS=$?
    if [ $RESTAURANT_STATUS -eq 0 ]; then
        log "[OK] Restaurant scraper completed successfully"
    else
        log "[ERROR] Restaurant scraper failed with status $RESTAURANT_STATUS"
    fi
else
    log "[ERROR] weekly_flhip_scraper.sh not found!"
    RESTAURANT_STATUS=1
fi

# -----------------------------------------------------------------------------
# STEP 2: Hotel Scraper (runs after restaurant completes)
# -----------------------------------------------------------------------------
log ""
log "[MASTER STEP 2] Running Hotel Scraper..."
log "============================================================"

if [ -f "$FLHIP_DIR/weekly_flhip_hotel_scraper.sh" ]; then
    bash "$FLHIP_DIR/weekly_flhip_hotel_scraper.sh" 2>&1 | tee -a "$MASTER_LOG"
    HOTEL_STATUS=$?
    if [ $HOTEL_STATUS -eq 0 ]; then
        log "[OK] Hotel scraper completed successfully"
    else
        log "[ERROR] Hotel scraper failed with status $HOTEL_STATUS"
    fi
else
    log "[ERROR] weekly_flhip_hotel_scraper.sh not found!"
    HOTEL_STATUS=1
fi

# -----------------------------------------------------------------------------
# STEP 3: Send Combined Email Report
# -----------------------------------------------------------------------------
log ""
log "[MASTER STEP 3] Sending Combined Email Report..."
log "============================================================"

cd "$FLHIP_DIR"
source "$FLHIP_DIR/venv/bin/activate"

# Calculate approximate contacts enriched (restaurants + hotels)
# Restaurant: ~350, Hotel: ~100
CONTACTS_ENRICHED=450

if [ -f "$FLHIP_DIR/email_weekly_report.py" ]; then
    python3 "$FLHIP_DIR/email_weekly_report.py" --days 7 --contacts-enriched $CONTACTS_ENRICHED 2>&1 | tee -a "$MASTER_LOG"
    EMAIL_STATUS=$?
    if [ $EMAIL_STATUS -eq 0 ]; then
        log "[OK] Email report sent successfully"
    else
        log "[ERROR] Email report failed with status $EMAIL_STATUS"
    fi
else
    log "[ERROR] email_weekly_report.py not found!"
    EMAIL_STATUS=1
fi

deactivate

# -----------------------------------------------------------------------------
# FINAL SUMMARY
# -----------------------------------------------------------------------------
log ""
log "============================================================"
log "FLHIP WEEKLY MASTER PIPELINE COMPLETE"
log "Finished: $(date)"
log "============================================================"
log ""
log "STATUS SUMMARY:"
log "   Restaurant Scraper: $([ $RESTAURANT_STATUS -eq 0 ] && echo 'SUCCESS' || echo 'FAILED')"
log "   Hotel Scraper:      $([ $HOTEL_STATUS -eq 0 ] && echo 'SUCCESS' || echo 'FAILED')"
log "   Email Report:       $([ $EMAIL_STATUS -eq 0 ] && echo 'SUCCESS' || echo 'FAILED')"
log ""
log "Log saved to: $MASTER_LOG"

# Exit with error if any step failed
if [ $RESTAURANT_STATUS -ne 0 ] || [ $HOTEL_STATUS -ne 0 ] || [ $EMAIL_STATUS -ne 0 ]; then
    exit 1
fi

exit 0
