#!/bin/bash

# Simple Jellyfin Migration to Docker Swarm Script
# Safely migrates from standalone container to Docker Swarm
# Created: $(date)

set -e

echo "=== Simple Jellyfin Migration to Docker Swarm ==="
echo ""

# Check current status
echo "Checking current Jellyfin status..."
if ssh root@omv800 "docker ps | grep -q jellyfin"; then
    echo "✅ Standalone Jellyfin container is running"
    STANDALONE_CONTAINER=$(ssh root@omv800 "docker ps -q -f name=jellyfin")
    echo "Container ID: $STANDALONE_CONTAINER"
else
    echo "❌ Standalone Jellyfin container not found"
    exit 1
fi

# Create safe storage directories
echo ""
echo "Creating safe storage directories..."
LOCAL_CONFIG_DIR="/srv/dev-disk-by-uuid-0f772f0b-917d-4337-a3c5-5cc5d3badac9/jellyfin-config"
LOCAL_CACHE_DIR="/srv/dev-disk-by-uuid-0f772f0b-917d-4337-a3c5-5cc5d3badac9/jellyfin-cache"

ssh root@omv800 "mkdir -p $LOCAL_CONFIG_DIR $LOCAL_CACHE_DIR"
ssh root@omv800 "chown -R 1000:1000 $LOCAL_CONFIG_DIR $LOCAL_CACHE_DIR"

# Copy configuration directly from running container
echo ""
echo "Copying configuration from running container..."
ssh root@omv800 "docker cp jellyfin:/config/. $LOCAL_CONFIG_DIR/"
ssh root@omv800 "docker cp jellyfin:/cache/. $LOCAL_CACHE_DIR/"

# Verify copy
echo "Verifying configuration copy..."
ssh root@omv800 "ls -la $LOCAL_CONFIG_DIR/ | head -10"
ssh root@omv800 "du -sh $LOCAL_CONFIG_DIR $LOCAL_CACHE_DIR"

# Stop standalone container
echo ""
echo "Stopping standalone Jellyfin container..."
ssh root@omv800 "docker stop jellyfin"
sleep 10

# Deploy Docker Swarm service
echo ""
echo "Deploying Jellyfin to Docker Swarm..."
cd /home/jonathan/Coding/HomeAudit
scp stacks/apps/jellyfin.yml root@omv800:/tmp/jellyfin.yml
ssh root@omv800 "docker stack deploy -c /tmp/jellyfin.yml jellyfin"

# Wait for service to start
echo "Waiting for Jellyfin service to start..."
sleep 60

# Check service status
echo ""
echo "Checking Docker Swarm service status..."
if ssh root@omv800 "docker service ls | grep -q jellyfin"; then
    echo "✅ Jellyfin Docker Swarm service is running"
    
    # Wait for container to be ready
    echo "Waiting for container to be ready..."
    sleep 30
    
    # Check container status
    CONTAINER_ID=$(ssh root@omv800 "docker ps -q -f name=jellyfin" | head -1)
    if [ -n "$CONTAINER_ID" ]; then
        echo "✅ Jellyfin container found: $CONTAINER_ID"
        
        # Check health status
        HEALTH=$(ssh root@omv800 "docker inspect $CONTAINER_ID --format='{{.State.Health.Status}}' 2>/dev/null || echo 'starting'")
        echo "Container health: $HEALTH"
        
        # Test accessibility
        echo "Testing Jellyfin accessibility..."
        if ssh root@omv800 "curl -s -o /dev/null -w '%{http_code}' http://localhost:8096" | grep -q "200\|302"; then
            echo "✅ Jellyfin is accessible on port 8096"
        else
            echo "⚠️  Jellyfin may not be accessible yet"
        fi
        
    else
        echo "❌ Jellyfin container not found"
        exit 1
    fi
else
    echo "❌ Jellyfin Docker Swarm service failed to start"
    exit 1
fi

# Clean up old standalone container
echo ""
echo "Cleaning up old standalone container..."
ssh root@omv800 "docker rm jellyfin 2>/dev/null || echo 'Container already removed'"

# Remove old Docker volumes
echo "Removing old Docker volumes..."
ssh root@omv800 "docker volume rm jellyfin-config jellyfin-cache 2>/dev/null || echo 'Volumes already removed'"

echo ""
echo "=== MIGRATION COMPLETE ==="
echo "✅ Jellyfin successfully migrated to Docker Swarm"
echo "✅ Configuration copied to safe local storage"
echo "✅ Storage moved to safe local drives"
echo "✅ Media remains on MergerFS (read-only)"
echo "✅ Caddy integration configured"
echo ""
echo "Storage locations:"
echo "  - Config: $LOCAL_CONFIG_DIR (safe local storage)"
echo "  - Cache: $LOCAL_CACHE_DIR (safe local storage)"
echo "  - Media: /srv/mergerfs/DataPool/ (MergerFS, read-only)"
echo ""
echo "To monitor Jellyfin:"
echo "  ssh root@omv800 'docker service ls | grep jellyfin'"
echo "  ssh root@omv800 'docker service logs jellyfin_jellyfin --tail 20'"
echo ""
echo "To access Jellyfin:"
echo "  https://jellyfin.pressmess.duckdns.org"
