#!/bin/bash

# BMC Hub Production Deployment Script
# Usage: ./updateto.sh v1.3.15

set -e  # Exit on any error

VERSION=$1

# This production deployment is designed for ROOTLESS Podman.
# Running with sudo will use a different Podman storage (rootful) and can make it look
# like "data disappeared" because volumes/networks are separate.
if [ "${EUID:-$(id -u)}" -eq 0 ]; then
    echo "❌ Fejl: Kør ikke dette script som root (sudo)."
    echo "   Brug i stedet: sudo -iu bmcadmin && cd /srv/podman/bmc_hub_v1.0 && ./updateto.sh $VERSION"
    exit 1
fi

PODMAN_COMPOSE_FILE="docker-compose.prod.yml"

if [ -z "$VERSION" ]; then
    echo "❌ Fejl: Ingen version angivet"
    echo "Usage: ./updateto.sh v1.3.15"
    exit 1
fi

# SAFETY CHECK: Verify we're on production server
CURRENT_IP=$(hostname -I | awk '{print $1}' 2>/dev/null || echo "unknown")
CURRENT_DIR=$(pwd)

if [[ "$CURRENT_IP" != "172.16.31.183" ]] && [[ "$CURRENT_DIR" != "/srv/podman/bmc_hub_v2" ]]; then
    echo "⚠️  ADVARSEL: Dette script skal kun køres på PRODUCTION serveren!"
    echo "   Forventet IP: 172.16.31.183"
    echo "   Forventet mappe: /srv/podman/bmc_hub_v2"
    echo "   Nuværende IP: $CURRENT_IP"
    echo "   Nuværende mappe: $CURRENT_DIR"
    echo ""
    read -p "Er du SIKKER på du vil fortsætte? (skriv 'JA' for at fortsætte): " CONFIRM
    if [ "$CONFIRM" != "JA" ]; then
        echo "❌ Deployment afbrudt"
        exit 1
    fi
fi

echo "🚀 Deploying BMC Hub version: $VERSION"
echo "================================"

# Check if .env exists
if [ ! -f ".env" ]; then
    echo "❌ Fejl: .env fil ikke fundet"
    exit 1
fi

if [ ! -f "$PODMAN_COMPOSE_FILE" ]; then
    echo "❌ Fejl: $PODMAN_COMPOSE_FILE ikke fundet i $(pwd)"
    echo "   Kør fra /srv/podman/bmc_hub_v1.0"
    exit 1
fi

# Load environment variables (DB credentials)
set -a
source .env
set +a

# Update RELEASE_VERSION in .env
echo "📝 Opdaterer .env med version $VERSION..."
if grep -q "^RELEASE_VERSION=" .env; then
    # Replace existing line
    sed -i.bak "s/^RELEASE_VERSION=.*/RELEASE_VERSION=$VERSION/" .env
else
    # Add if missing
    echo "RELEASE_VERSION=$VERSION" >> .env
fi

echo "✅ .env opdateret"

# Stop containers
echo ""
echo "⏹️  Stopper containere..."
podman-compose -f "$PODMAN_COMPOSE_FILE" down

# Pull/rebuild with new version
echo ""
echo "🔨 Bygger nyt image med version $VERSION..."
podman-compose -f "$PODMAN_COMPOSE_FILE" up -d --build

# Sync migrations from container to host
echo ""
echo "📁 Syncer migrations fra container til host..."
SYNC_OK=false
for i in {1..20}; do
    rm -rf ./migrations_temp
    if podman cp bmc-hub-api-prod:/app/migrations ./migrations_temp 2>/dev/null; then
        rm -rf ./migrations
        mv ./migrations_temp ./migrations
        chmod -R 755 ./migrations
        SYNC_OK=true
        echo "✅ Migrations synced"
        break
    fi
    echo "⏳ Venter på API container for migration sync... ($i/20)"
    sleep 2
done

if [ "$SYNC_OK" != "true" ]; then
    echo "❌ Fejl: Kunne ikke sync'e migrations fra bmc-hub-api-prod:/app/migrations"
    echo "   Afbryder for at undgå kørsel af gamle migrations"
    exit 1
fi

if [ ! -f "./migrations/138_customers_economic_unique_constraint.sql" ]; then
    echo "❌ Fejl: Forventet migration 138 mangler efter sync"
    exit 1
fi

# Wait a bit for startup
echo ""
echo "⏳ Venter på container startup..."
sleep 5

# Database migrations
echo ""
echo "🧱 Database migrationer"
echo "   NOTE: Scriptet kører ikke længere en hardcoded enkelt-migration automatisk."
echo "   Brug migrations-UI'en i BMC Hub, eller sæt RUN_MIGRATIONS=true for at køre alle .sql i /docker-entrypoint-initdb.d/ i sorteret rækkefølge."

if [ "${RUN_MIGRATIONS:-false}" = "true" ]; then
    if [ -z "$POSTGRES_USER" ] || [ -z "$POSTGRES_DB" ]; then
        echo "❌ Fejl: POSTGRES_USER/POSTGRES_DB mangler i .env"
        exit 1
    fi

    for i in {1..30}; do
        if podman exec bmc-hub-postgres-prod pg_isready -U "$POSTGRES_USER" >/dev/null 2>&1; then
            break
        fi
        echo "⏳ Venter på postgres... ($i/30)"
        sleep 2
    done

    echo "📄 Kører alle migrations fra /docker-entrypoint-initdb.d (sorteret)..."
    podman exec bmc-hub-postgres-prod sh -lc "ls -1 /docker-entrypoint-initdb.d/*.sql 2>/dev/null | sort" \
      | while read -r file; do
            [ -z "$file" ] && continue
            echo "➡️  $file"
            podman exec -i bmc-hub-postgres-prod psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" -f "$file"
        done

    echo "✅ Migrations kørt"
else
    echo "ℹ️  RUN_MIGRATIONS=false (default)"
fi

# Show logs
echo ""
echo "📋 Logs fra startup:"
echo "================================"
podman logs --tail 50 bmc-hub-api-prod

echo ""
echo "✅ Deployment fuldført!"
echo ""
echo "🔍 Tjek status med:"
echo "   podman-compose -f $PODMAN_COMPOSE_FILE ps"
echo "   podman logs -f bmc-hub-api-prod"
echo ""
echo "🌐 Test health endpoint:"
echo "   curl http://localhost:8000/health"
echo ""
echo "📊 Sync kunder fra e-conomic:"
echo "   curl -X POST http://localhost:8000/api/v1/system/sync/economic"
echo ""
echo "🔗 Link vTiger til kunder:"
echo "   curl -X POST http://localhost:8000/api/v1/system/sync/vtiger"
