#!/usr/bin/env bash
# setup_metabase_volume
# ---------------------
# Copia registry.db al contenedor Docker de Metabase.
# Compone: assert_file_exists + assert_command_exists +
#          assert_docker_container_running + docker_cp_file
#
# USO:
#   ./setup_metabase_volume.sh [REGISTRY_DB_PATH] [CONTAINER_NAME] [DEST_PATH]
#
# ARGUMENTOS (opcionales, con defaults):
#   REGISTRY_DB_PATH  Ruta local al registry.db
#                     Default: <raiz_del_registry>/registry.db
#   CONTAINER_NAME    Nombre del contenedor Docker de Metabase
#                     Default: metabase
#   DEST_PATH         Ruta destino dentro del contenedor
#                     Default: /registry.db

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REGISTRY_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"

source "$REGISTRY_ROOT/bash/functions/shell/assert_file_exists.sh"
source "$REGISTRY_ROOT/bash/functions/shell/assert_command_exists.sh"
source "$REGISTRY_ROOT/bash/functions/infra/assert_docker_container_running.sh"
source "$REGISTRY_ROOT/bash/functions/infra/docker_cp_file.sh"

REGISTRY_DB_PATH="${1:-$REGISTRY_ROOT/registry.db}"
CONTAINER_NAME="${2:-metabase}"
DEST_PATH="${3:-/registry.db}"

echo "[setup_metabase_volume] Configuracion:"
echo "  registry.db local : $REGISTRY_DB_PATH"
echo "  contenedor Docker  : $CONTAINER_NAME"
echo "  ruta en contenedor : $DEST_PATH"
echo ""

# 1. Verificar archivo local
local_size=$(assert_file_exists "$REGISTRY_DB_PATH")
echo "[setup_metabase_volume] Archivo local encontrado: ${local_size} bytes"

# 2. Verificar que docker esta disponible
assert_command_exists docker
echo "[setup_metabase_volume] docker disponible en PATH."

# 3. Verificar que el contenedor esta corriendo
if ! assert_docker_container_running "$CONTAINER_NAME"; then
    echo "" >&2
    echo "Contenedores activos:" >&2
    docker ps --format "  {{.Names}}\t{{.Status}}\t{{.Image}}" >&2
    echo "" >&2
    echo "Si el contenedor se llama diferente, pasa el nombre como segundo argumento:" >&2
    echo "  ./setup_metabase_volume.sh $REGISTRY_DB_PATH <nombre_contenedor>" >&2
    exit 1
fi
echo "[setup_metabase_volume] Contenedor '$CONTAINER_NAME' encontrado y activo."

# 4. Copiar archivo y verificar tamaños
echo "[setup_metabase_volume] Copiando $REGISTRY_DB_PATH -> ${CONTAINER_NAME}:${DEST_PATH} ..."
result=$(docker_cp_file "$REGISTRY_DB_PATH" "$CONTAINER_NAME" "$DEST_PATH")

remote_size=$(echo "$result" | grep -o '"remote_size":[0-9]*' | cut -d: -f2)
echo "[setup_metabase_volume] OK Copia completada y verificada."
echo "[setup_metabase_volume] Tamanio: ${local_size} bytes (local) = ${remote_size} bytes (remoto)"

echo ""
echo "---------------------------------------------------------------------"
echo "registry.db disponible en el contenedor como: $DEST_PATH"
echo ""
echo "Ahora ejecuta main.py con:"
echo ""
echo "  METABASE_ADMIN_PASSWORD=<password> \\"
echo "  REGISTRY_DB_PATH=${DEST_PATH} \\"
echo "  python apps/metabase_registry/main.py"
echo ""
echo "O bien:"
echo ""
echo "  python apps/metabase_registry/main.py \\"
echo "    --admin-password <password> \\"
echo "    --registry-db-path ${DEST_PATH}"
echo "---------------------------------------------------------------------"
