#!/bin/bash
set -e

# HKIA Scraper Services Installation Script
# This script replaces old hvac-content services with new hkia-scraper services

echo "============================================================"
echo "HKIA Content Scraper Services Installation"
echo "============================================================"

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

# Function to print colored output
print_status() {
    echo -e "${GREEN}✅${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠️${NC} $1"
}

print_error() {
    echo -e "${RED}❌${NC} $1"
}

print_info() {
    echo -e "${BLUE}ℹ️${NC} $1"
}

# Check if running as root
if [[ $EUID -eq 0 ]]; then
   print_error "This script should not be run as root. Run it as the user 'ben' and it will use sudo when needed."
   exit 1
fi

# Check if we're in the right directory
if [[ ! -f "CLAUDE.md" ]] || [[ ! -d "systemd" ]]; then
    print_error "Please run this script from the hvac-kia-content project root directory"
    exit 1
fi

# Check if systemd files exist
required_files=(
    "systemd/hkia-scraper.service"
    "systemd/hkia-scraper.timer"
    "systemd/hkia-scraper-nas.service"
    "systemd/hkia-scraper-nas.timer"
)

for file in "${required_files[@]}"; do
    if [[ ! -f "$file" ]]; then
        print_error "Required file not found: $file"
        exit 1
    fi
done

print_info "All required service files found"

echo ""
echo "============================================================"
echo "STEP 1: Stopping and Disabling Old Services"
echo "============================================================"

# List of old services to stop and disable
old_services=(
    "hvac-content-images-8am.timer"
    "hvac-content-images-12pm.timer"
    "hvac-content-8am.timer"
    "hvac-content-12pm.timer"
    "hvac-content-images-8am.service"
    "hvac-content-images-12pm.service"
    "hvac-content-8am.service"
    "hvac-content-12pm.service"
)

for service in "${old_services[@]}"; do
    if systemctl is-active --quiet "$service" 2>/dev/null; then
        print_info "Stopping $service..."
        sudo systemctl stop "$service"
        print_status "Stopped $service"
    else
        print_info "$service is not running"
    fi
    
    if systemctl is-enabled --quiet "$service" 2>/dev/null; then
        print_info "Disabling $service..."
        sudo systemctl disable "$service"
        print_status "Disabled $service"
    else
        print_info "$service is not enabled"
    fi
done

echo ""
echo "============================================================"
echo "STEP 2: Installing New HKIA Services"
echo "============================================================"

# Copy service files to systemd directory
print_info "Copying service files to /etc/systemd/system/..."
sudo cp systemd/hkia-scraper.service /etc/systemd/system/
sudo cp systemd/hkia-scraper.timer /etc/systemd/system/
sudo cp systemd/hkia-scraper-nas.service /etc/systemd/system/
sudo cp systemd/hkia-scraper-nas.timer /etc/systemd/system/

print_status "Service files copied successfully"

# Reload systemd daemon
print_info "Reloading systemd daemon..."
sudo systemctl daemon-reload
print_status "Systemd daemon reloaded"

echo ""
echo "============================================================"
echo "STEP 3: Enabling New Services"
echo "============================================================"

# New services to enable
new_services=(
    "hkia-scraper.service"
    "hkia-scraper.timer"
    "hkia-scraper-nas.service"
    "hkia-scraper-nas.timer"
)

for service in "${new_services[@]}"; do
    print_info "Enabling $service..."
    sudo systemctl enable "$service"
    print_status "Enabled $service"
done

echo ""
echo "============================================================"
echo "STEP 4: Starting Timers"
echo "============================================================"

# Start the timers (services will be triggered by timers)
timers=("hkia-scraper.timer" "hkia-scraper-nas.timer")

for timer in "${timers[@]}"; do
    print_info "Starting $timer..."
    sudo systemctl start "$timer"
    print_status "Started $timer"
done

echo ""
echo "============================================================"
echo "STEP 5: Verification"
echo "============================================================"

# Check status of new services
print_info "Checking status of new services..."

for timer in "${timers[@]}"; do
    echo ""
    print_info "Status of $timer:"
    sudo systemctl status "$timer" --no-pager -l
done

echo ""
echo "============================================================"
echo "STEP 6: Schedule Summary"
echo "============================================================"

print_info "New HKIA Services Schedule (Atlantic Daylight Time):"
echo "  📅 Main Scraping: 8:00 AM and 12:00 PM"
echo "  📁 NAS Sync: 8:30 AM and 12:30 PM (30min after scraping)"
echo ""
print_info "Active Sources: WordPress, MailChimp RSS, Podcast RSS, YouTube, Instagram"
print_warning "TikTok scraper is disabled (not working as designed)"

echo ""
echo "============================================================"
echo "INSTALLATION COMPLETE"
echo "============================================================"

print_status "HKIA scraper services have been successfully installed and started!"
print_info "Next scheduled run will be at the next 8:00 AM or 12:00 PM ADT"

echo ""
print_info "Useful commands:"
echo "  sudo systemctl status hkia-scraper.timer"
echo "  sudo systemctl status hkia-scraper-nas.timer" 
echo "  sudo journalctl -f -u hkia-scraper.service"
echo "  sudo journalctl -f -u hkia-scraper-nas.service"

# Show next scheduled runs
echo ""
print_info "Next scheduled runs:"
sudo systemctl list-timers | grep hkia || print_warning "No upcoming runs shown (timers may need a moment to register)"

echo ""
print_status "Installation script completed successfully!"