#!/bin/bash
# Deploy Public Lists Integration (v2.0.0)
# Run on AlmaLinux 9 server after git pull

set -e

echo "=================================="
echo "PUBLIC LISTS DEPLOYMENT - v2.0.0"
echo "=================================="

# 1. Database Migration
echo -e "\n[1/5] Running database migration..."
sudo -u postgres psql -d ids_system -f deployment/migrations/006_add_public_lists.sql
echo "✓ Migration 006 applied"

# 2. Seed default lists
echo -e "\n[2/5] Seeding default public lists..."
cd python_ml/list_fetcher
DATABASE_URL=$DATABASE_URL python seed_lists.py
cd ../..
echo "✓ Default lists seeded"

# 3. Install systemd services
echo -e "\n[3/5] Installing systemd services..."
sudo cp deployment/systemd/ids-list-fetcher.service /etc/systemd/system/
sudo cp deployment/systemd/ids-list-fetcher.timer /etc/systemd/system/
sudo systemctl daemon-reload
echo "✓ Systemd services installed"

# 4. Enable and start
echo -e "\n[4/5] Enabling services..."
sudo systemctl enable ids-list-fetcher.timer
sudo systemctl start ids-list-fetcher.timer
echo "✓ Timer enabled (10-minute intervals)"

# 5. Initial sync
echo -e "\n[5/5] Running initial sync..."
sudo systemctl start ids-list-fetcher.service
echo "✓ Initial sync triggered"

echo -e "\n=================================="
echo "DEPLOYMENT COMPLETE"
echo "=================================="
echo ""
echo "Verify:"
echo "  journalctl -u ids-list-fetcher -n 50"
echo "  systemctl status ids-list-fetcher.timer"
echo ""
echo "Check UI: http://your-server/public-lists"
echo ""
