#!/bin/bash

# Configuration
PROJECT="infra-cms"
SCHEMA_PATH="./packages/cms-infra/schema/snapshot.yaml"
CMD_PREFIX="docker-compose -f packages/cms-infra/docker-compose.yml"

# Detect local container
LOCAL_CONTAINER=$($CMD_PREFIX ps -q $PROJECT)

if [ -z "$LOCAL_CONTAINER" ]; then
    echo "❌ Local $PROJECT container not found. Is it running?"
    exit 1
fi

echo "📸 Creating schema snapshot for local $PROJECT..."
# Note: we save it to the mounted volume path inside the container
docker exec "$LOCAL_CONTAINER" npx directus schema snapshot -y /directus/schema/snapshot.yaml

echo "🛠️ Repairing snapshot for Postgres compatibility..."
python3 ./scripts/fix_snapshot_v3.py

echo "✅ Snapshot saved and repaired at $SCHEMA_PATH"
