#!/bin/bash
# =============================================================================
# TRIPLESTORE LOADER — Le Monde Sémantique · AI Act Audit Platform
# Fichier : triplestore/loader.sh
# Usage   : ./loader.sh <fichier.ttl> <graph-uri> [dataset]
#
# Exemples :
#   ./loader.sh ../data/graphe-03-aiact-audit.ttl \
#       http://lemondesemantique.fr/graph/aiact-audit
#
#   ./loader.sh ../data/graphe-client-001.ttl \
#       http://lemondesemantique.fr/graph/aiact-audit/client-001
# =============================================================================

set -euo pipefail

# --- Config (à aligner avec triplestore/config.php) -------------------------
FUSEKI_BASE="http://localhost:3030"
DATASET="${3:-aiact}"
FUSEKI_DATA_URL="${FUSEKI_BASE}/${DATASET}/data"

# --- Paramètres --------------------------------------------------------------
TTL_FILE="${1:-}"
GRAPH_URI="${2:-}"

if [[ -z "$TTL_FILE" || -z "$GRAPH_URI" ]]; then
    echo "Usage: $0 <fichier.ttl> <graph-uri> [dataset]"
    exit 1
fi

if [[ ! -f "$TTL_FILE" ]]; then
    echo "❌ Fichier introuvable : $TTL_FILE"
    exit 1
fi

# --- Validation SHACL avant chargement (si jena-shacl disponible) -----------
if command -v shacl &> /dev/null; then
    echo "🔍 Validation SHACL en cours..."
    shacl validate --shapes "$TTL_FILE" --data "$TTL_FILE" && echo "✅ SHACL : conforme"
fi

# --- Suppression de l'ancien graphe (idempotent) ----------------------------
echo "🗑  Suppression de l'ancien graphe : $GRAPH_URI"
curl -s -X DELETE "${FUSEKI_DATA_URL}?graph=$(python3 -c "import urllib.parse; print(urllib.parse.quote('$GRAPH_URI'))")" \
     -w "HTTP %{http_code}\n" || true

# --- Chargement du nouveau graphe -------------------------------------------
echo "📤 Chargement : $TTL_FILE → $GRAPH_URI"
HTTP_CODE=$(curl -s -o /tmp/fuseki_response.txt -w "%{http_code}" \
    -X POST "${FUSEKI_DATA_URL}?graph=$(python3 -c "import urllib.parse; print(urllib.parse.quote('$GRAPH_URI'))")" \
    -H "Content-Type: text/turtle" \
    --data-binary "@${TTL_FILE}")

if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 300 ]]; then
    echo "✅ Chargement réussi (HTTP $HTTP_CODE)"
    echo "   Graphe : $GRAPH_URI"
    echo "   Dataset : $DATASET"
else
    echo "❌ Erreur HTTP $HTTP_CODE"
    cat /tmp/fuseki_response.txt
    exit 1
fi

# --- Vérification : compte les triples chargés ------------------------------
echo "📊 Vérification..."
COUNT=$(curl -s -G "${FUSEKI_BASE}/${DATASET}/sparql" \
    --data-urlencode "query=SELECT (COUNT(*) AS ?n) WHERE { GRAPH <${GRAPH_URI}> { ?s ?p ?o } }" \
    -H "Accept: application/sparql-results+json" | \
    python3 -c "import sys,json; d=json.load(sys.stdin); print(d['results']['bindings'][0]['n']['value'])" 2>/dev/null || echo "?")

echo "   Triples chargés : $COUNT"
echo ""
echo "🏁 Commande curl directe si besoin :"
echo "   curl -X POST '${FUSEKI_DATA_URL}?graph=${GRAPH_URI}' \\"
echo "        -H 'Content-Type: text/turtle' \\"
echo "        --data-binary @${TTL_FILE}"
