#!/bin/bash
# BMC Hub v1.0.0 - Production Server Setup
# Kopier og kør denne fil på production serveren

set -e

echo "🚀 BMC Hub v1.0.0 Production Setup"
echo "=================================="
echo ""

# Step 1: Opret directory
echo "📁 Step 1: Opretter deployment directory..."
mkdir -p /srv/podman/bmc_hub_v1.0
cd /srv/podman/bmc_hub_v1.0

# Step 2: Download deployment script
echo "⬇️  Step 2: Downloader deployment script..."
curl -H "Authorization: token 58419ee1f9503a95b58e56448db6f77a6caddff6" \
     https://g.bmcnetworks.dk/api/v1/repos/ct/bmc_hub/raw/scripts/deploy_production.sh?ref=v1.0.0 \
     -o setup.sh
chmod +x setup.sh

# Step 3: Opret .env fil
echo "⚙️  Step 3: Opretter .env konfiguration..."
cat > .env << 'EOF'
# Release
RELEASE_VERSION=v1.0.0
GITEA_URL=https://g.bmcnetworks.dk
GITHUB_TOKEN=58419ee1f9503a95b58e56448db6f77a6caddff6
GITHUB_REPO=ct/bmc_hub

# Database - SKIFT PASSWORD!
POSTGRES_USER=bmc_hub_prod
POSTGRES_PASSWORD=CHANGE_THIS_TO_STRONG_PASSWORD
POSTGRES_DB=bmc_hub_prod
DATABASE_URL=postgresql://bmc_hub_prod:CHANGE_THIS_TO_STRONG_PASSWORD@postgres:5432/bmc_hub_prod

# Security - GENERER NY SECRET KEY!
# Run: python3 -c "import secrets; print(secrets.token_urlsafe(32))"
SECRET_KEY=GENERATE_RANDOM_SECRET_KEY_HERE

# API
API_HOST=0.0.0.0
API_PORT=8000
API_RELOAD=false
CORS_ORIGINS=http://172.16.31.183:8001
LOG_LEVEL=WARNING
LOG_FILE=logs/app.log

# Safety Switches (BEHOLD true!)
ECONOMIC_READ_ONLY=true
ECONOMIC_DRY_RUN=true
TIMETRACKING_VTIGER_READ_ONLY=true
TIMETRACKING_VTIGER_DRY_RUN=true
TIMETRACKING_ECONOMIC_READ_ONLY=true
TIMETRACKING_ECONOMIC_DRY_RUN=true

# Ollama AI
OLLAMA_ENDPOINT=http://ai_direct.cs.blaahund.dk
OLLAMA_MODEL=qwen2.5-coder:7b

# e-conomic (Udfyld med rigtige credentials)
ECONOMIC_API_URL=https://restapi.e-conomic.com
ECONOMIC_APP_SECRET_TOKEN=your_app_secret_token_here
ECONOMIC_AGREEMENT_GRANT_TOKEN=your_agreement_grant_token_here

# vTiger CRM
VTIGER_URL=https://bmcnetworks.od2.vtiger.com
VTIGER_USERNAME=ct@bmcnetworks.dk
VTIGER_API_KEY=bD8cW8zRFuKpPZ2S

# Time Tracking
TIMETRACKING_DEFAULT_HOURLY_RATE=1200.00
TIMETRACKING_AUTO_ROUND=true
TIMETRACKING_ROUND_INCREMENT=0.5
TIMETRACKING_ROUND_METHOD=up
EOF

echo ""
echo "✅ Setup klar!"
echo ""
echo "⚠️  VIGTIGT - REDIGER .env FIL NU:"
echo "   1. Skift POSTGRES_PASSWORD til stærk password"
echo "   2. Generer SECRET_KEY: python3 -c \"import secrets; print(secrets.token_urlsafe(32))\""
echo "   3. Opdater CORS_ORIGINS til dit domain"
echo "   4. Udfyld e-conomic credentials (hvis relevant)"
echo ""
echo "Rediger med: nano .env"
echo ""
read -p "Tryk ENTER når du har redigeret .env filen..."

# Step 4: Download filer fra Gitea
echo ""
echo "📥 Step 4: Downloader application filer..."
./setup.sh

# Step 5: Start services
echo ""
echo "🚢 Step 5: Starter Docker/Podman services..."
if command -v podman-compose &> /dev/null; then
    podman-compose up -d --build
elif command -v docker-compose &> /dev/null; then
    docker-compose up -d --build
else
    echo "❌ Hverken podman-compose eller docker-compose fundet!"
    echo "Installer en af dem og kør: podman-compose up -d --build"
    exit 1
fi

# Step 6: Vent på services
echo ""
echo "⏳ Venter på at services starter..."
sleep 10

# Step 7: Verificer
echo ""
echo "✅ Step 6: Verificerer deployment..."
if curl -f http://localhost:8000/health &> /dev/null; then
    echo ""
    echo "🎉 SUCCESS! BMC Hub kører på http://localhost:8000"
    echo ""
    curl http://localhost:8000/health | python3 -m json.tool
    echo ""
    echo "📊 Se logs med: podman-compose logs -f"
    echo "📦 Se containers: podman ps"
else
    echo ""
    echo "⚠️  Health check fejlede. Check logs:"
    echo "   podman-compose logs -f"
fi
