#!/usr/bin/env bash
# === Post-deploy OCPI + Everest configuration for Cariflex ===
# Run this AFTER docker compose up succeeds

set -e

CSMS_URL="http://localhost:8081"
GRAPHQL_URL="http://localhost:8090/v1/graphql"
DB_CONTAINER="cariflex-citrineos-db"

echo "=== Step 1: Seed OCPI database ==="
# Run inside the OCPI container to seed default tenant + emsp
docker exec cariflex-ocpi npm run seed-db 2>&1 || echo "Seed may have run before, continuing..."

echo "=== Step 2: Verify Citiveness ==="
# Check CitrineOS Core is healthy
curl -sf "${CSMS_URL}/ocpp/health" && echo "CitrineOS Core: OK" || echo "CitrineOS Core: UNHEALTHY"

echo "=== Step 3: Configure EVerest Manager OCPP target ==="
# The manager already points to ws://cariflex-citrineos-server:8080/cp001 via Dockerfile
# Check manager is running
docker ps --filter "name=cariflex-everest-manager" --format "{{.Status}}" && echo "Everest Manager: running" || echo "Everest Manager: NOT RUNNING"

echo "=== Step 4: Verify UI connectivity ==="
echo "  Everest NodeRED UI: http://localhost:1880/ui/"
echo "  Everest OCPP Logs:   http://localhost:8888"
echo "  CitrineOS UI:        https://citrineos.digitribe.fr"
echo "  Hasura Console:      https://hasura.digitribe.fr"
echo ""
echo "=== DONE ==="
