#!/bin/bash
#
# HKIA - Production Deployment Script
# Sets up systemd services, directories, and configuration
#

set -e

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

# Production paths
PROD_DIR="/opt/hvac-kia-content"
SERVICE_USER="hvac-content"
REPO_DIR="$(pwd)"

# Print colored output
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

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

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

# Check if running as root
check_root() {
    if [[ $EUID -ne 0 ]]; then
        print_error "This script must be run as root (use sudo)"
        exit 1
    fi
}

# Create service user
setup_user() {
    print_status "Setting up service user..."
    
    if ! id "$SERVICE_USER" &>/dev/null; then
        useradd --system --shell /bin/bash --home-dir "$PROD_DIR" --create-home "$SERVICE_USER"
        print_success "Created service user: $SERVICE_USER"
    else
        print_warning "Service user $SERVICE_USER already exists"
    fi
}

# Setup production directory
setup_directories() {
    print_status "Setting up production directories..."
    
    # Create production directory
    mkdir -p "$PROD_DIR"
    mkdir -p "$PROD_DIR/data"
    mkdir -p "$PROD_DIR/logs"
    mkdir -p "$PROD_DIR/backups"
    mkdir -p "$PROD_DIR/venv"
    
    # Create NAS mount point (if doesn't exist)
    mkdir -p "/mnt/nas/hkia"
    
    # Copy application files
    cp -r "$REPO_DIR/src" "$PROD_DIR/"
    cp -r "$REPO_DIR/config" "$PROD_DIR/"
    cp "$REPO_DIR/pyproject.toml" "$PROD_DIR/"
    cp "$REPO_DIR/run_production.py" "$PROD_DIR/"
    cp "$REPO_DIR/production_backlog_capture.py" "$PROD_DIR/"
    cp "$REPO_DIR/automated_backlog_capture.py" "$PROD_DIR/"
    
    # Copy environment template
    if [[ -f "$REPO_DIR/.env.production" ]]; then
        cp "$REPO_DIR/.env.production" "$PROD_DIR/.env.template"
        print_warning "Remember to configure $PROD_DIR/.env with actual credentials"
    fi
    
    # Set ownership
    chown -R "$SERVICE_USER:$SERVICE_USER" "$PROD_DIR"
    
    print_success "Production directories configured"
}

# Install Python dependencies
setup_python() {
    print_status "Setting up Python environment..."
    
    # Install uv if not available
    if ! command -v uv &> /dev/null; then
        print_status "Installing uv package manager..."
        curl -LsSf https://astral.sh/uv/install.sh | sh
        source ~/.bashrc
    fi
    
    # Switch to service user for Python setup
    sudo -u "$SERVICE_USER" bash << EOF
cd "$PROD_DIR"
export PATH="/home/$SERVICE_USER/.local/bin:\$PATH"

# Create virtual environment and install dependencies
uv venv venv
source venv/bin/activate
uv pip install -e .

# Install playwright browsers
if uv pip list | grep -q playwright; then
    playwright install chromium
fi
EOF
    
    print_success "Python environment configured"
}

# Install systemd services
install_services() {
    print_status "Installing systemd services..."
    
    # Copy systemd files
    cp "$REPO_DIR/systemd/"*.service /etc/systemd/system/
    cp "$REPO_DIR/systemd/"*.timer /etc/systemd/system/
    
    # Update service files with correct paths and user
    for service_file in /etc/systemd/system/hvac-*.service; do
        sed -i "s|/home/ben/dev/hvac-kia-content|$PROD_DIR|g" "$service_file"
        sed -i "s|User=ben|User=$SERVICE_USER|g" "$service_file"
        sed -i "s|Group=ben|Group=$SERVICE_USER|g" "$service_file"
    done
    
    # Reload systemd
    systemctl daemon-reload
    
    # Enable services (but don't start yet)
    systemctl enable hvac-content-aggregator.timer
    systemctl enable hvac-monitoring.timer
    systemctl enable hvac-tiktok-captions.timer
    
    print_success "Systemd services installed and enabled"
}

# Setup monitoring
setup_monitoring() {
    print_status "Setting up monitoring..."
    
    # Copy monitoring files
    cp -r "$REPO_DIR/monitoring" "$PROD_DIR/"
    chown -R "$SERVICE_USER:$SERVICE_USER" "$PROD_DIR/monitoring"
    
    # Create monitoring dashboard
    sudo -u "$SERVICE_USER" bash << EOF
cd "$PROD_DIR"
source venv/bin/activate
python monitoring/setup_monitoring.py
EOF
    
    print_success "Monitoring configured"
}

# Create logrotate configuration
setup_logrotate() {
    print_status "Setting up log rotation..."
    
    cat > /etc/logrotate.d/hvac-content << EOF
$PROD_DIR/logs/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    create 644 $SERVICE_USER $SERVICE_USER
    postrotate
        systemctl reload hvac-content-aggregator.service || true
    endscript
}
EOF
    
    print_success "Log rotation configured"
}

# Verify installation
verify_installation() {
    print_status "Verifying installation..."
    
    # Check Python environment
    if sudo -u "$SERVICE_USER" "$PROD_DIR/venv/bin/python" -c "import src.orchestrator; print('✓ Python modules OK')"; then
        print_success "Python environment verified"
    else
        print_error "Python environment verification failed"
        return 1
    fi
    
    # Check systemd services
    for service in hvac-content-aggregator hvac-monitoring hvac-tiktok-captions; do
        if systemctl is-enabled "${service}.timer" &>/dev/null; then
            print_success "Service ${service}.timer is enabled"
        else
            print_error "Service ${service}.timer is not enabled"
            return 1
        fi
    done
    
    # Check directories
    for dir in data logs backups; do
        if [[ -d "$PROD_DIR/$dir" ]]; then
            print_success "Directory $dir exists"
        else
            print_error "Directory $dir missing"
            return 1
        fi
    done
    
    print_success "Installation verification complete"
}

# Main deployment function
main() {
    print_status "Starting HKIA production deployment..."
    echo
    
    check_root
    setup_user
    setup_directories
    setup_python
    install_services
    setup_monitoring
    setup_logrotate
    verify_installation
    
    echo
    print_success "🎉 Production deployment complete!"
    echo
    print_warning "Next steps:"
    echo "1. Configure $PROD_DIR/.env with actual credentials"
    echo "2. Test the installation: sudo -u $SERVICE_USER $PROD_DIR/venv/bin/python $PROD_DIR/run_production.py --dry-run"
    echo "3. Start services: sudo systemctl start hvac-content-aggregator.timer"
    echo "4. Monitor logs: sudo journalctl -u hvac-content-aggregator.service -f"
    echo "5. Check monitoring dashboard: http://localhost:8080"
    echo
}

# Run main function
main "$@"