#!/usr/bin/env bash
# rig-extract-phase0.sh
# Extrait le core Epic Fight nécessaire pour le RIG system TiedUp.
# Voir docs/plans/rig/EXTRACTION.md §9 pour détails.
#
# NE PAS utiliser "set -e" — certains cp peuvent échouer (fichiers non
# critiques, 2>/dev/null || true) et on veut continuer.
#
# Lancer depuis la racine du projet.

set -u

SRC="docs/ModSources/epicfight-1.20.1/src/main/java/yesman/epicfight"
DST="src/main/java/com/tiedup/remake/rig"

if [ ! -d "$SRC" ]; then
    echo "ERROR: Epic Fight source not found at $SRC"
    exit 1
fi

echo "=== rig-extract-phase0.sh ==="
echo "Source : $SRC"
echo "Cible  : $DST"
echo ""

echo "=== 0. Structure des packages ==="
mkdir -p "$DST"/{math,armature,armature/types,anim,anim/types,anim/property,anim/client,anim/client/property,mesh,mesh/transformer,cloth,asset,event,patch,render,render/compute,registry,bridge,tick,mixin,util,util/datastruct,exception}

echo "=== 1. Math utils ==="
cp -v "$SRC"/api/utils/math/*.java "$DST/math/"

echo ""
echo "=== 2. Armature (Armature + Joint + JointTransform) ==="
cp -v "$SRC"/api/model/Armature.java "$DST/armature/"
cp -v "$SRC"/api/animation/Joint.java "$DST/armature/"
cp -v "$SRC"/api/animation/JointTransform.java "$DST/armature/"

echo ""
echo "=== 3. Animation core + LivingMotion + ServerAnimator ==="
for f in Animator AnimationPlayer AnimationClip AnimationManager AnimationVariables \
         SynchedAnimationVariableKey SynchedAnimationVariableKeys Keyframe Pose TransformSheet \
         LivingMotion LivingMotions ServerAnimator; do
    cp -v "$SRC/api/animation/$f.java" "$DST/anim/" 2>/dev/null || echo "  (skip : $f.java non trouvé)"
done
cp -v "$SRC"/api/animation/property/*.java "$DST/anim/property/" 2>/dev/null

echo ""
echo "=== 4. Animation types (filtrés + stubs combat pour JsonAssetLoader) ==="
for f in DynamicAnimation StaticAnimation LinkAnimation \
         ConcurrentLinkAnimation LayerOffAnimation EntityState \
         ActionAnimation AttackAnimation MainFrameAnimation; do
    # ActionAnimation/AttackAnimation/MainFrameAnimation seront simplifiés
    # manuellement en stubs (retirer le combat, garder signatures).
    cp -v "$SRC/api/animation/types/$f.java" "$DST/anim/types/" 2>/dev/null || echo "  (skip : $f.java non trouvé)"
done

echo ""
echo "=== 5. Animation client ==="
cp -v "$SRC"/api/client/animation/*.java "$DST/anim/client/" 2>/dev/null
cp -v "$SRC"/api/client/animation/property/*.java "$DST/anim/client/property/" 2>/dev/null
# TrailInfo hors scope
rm -f "$DST/anim/client/property/TrailInfo.java"

echo ""
echo "=== 6. Mesh ==="
cp -v "$SRC"/api/client/model/*.java "$DST/mesh/" 2>/dev/null
# Retirer ItemSkinsReloadListener (cosmetics combat)
rm -f "$DST/mesh/ItemSkinsReloadListener.java"

echo ""
echo "=== 7. Cloth (absorbé Phase 0 — StaticMesh en dépend) ==="
cp -v "$SRC"/api/client/physics/AbstractSimulator.java "$DST/cloth/" 2>/dev/null
cp -v "$SRC"/api/client/physics/cloth/*.java "$DST/cloth/" 2>/dev/null

echo ""
echo "=== 8. Asset loader ==="
cp -v "$SRC"/api/asset/*.java "$DST/asset/" 2>/dev/null

echo ""
echo "=== 9. Forge events ==="
for f in PatchedRenderersEvent PrepareModelEvent RegisterResourceLayersEvent; do
    cp -v "$SRC/api/client/forgeevent/$f.java" "$DST/event/" 2>/dev/null || echo "  (skip : $f.java non trouvé)"
done

echo ""
echo "=== 10. RenderTypes ==="
cp -v "$SRC"/client/renderer/EpicFightRenderTypes.java "$DST/render/TiedUpRenderTypes.java" 2>/dev/null
echo "  NOTE: RenderEngine.Events à extraire manuellement dans render/TiedUpRenderEngine.java"
echo "  NOTE: TiedUpRigConstants.java à créer manuellement (factory ANIMATOR_PROVIDER + isPhysicalClient)"

echo ""
echo "=== 11. ComputeShader stubs (à créer manuellement, no-op) ==="
echo "  NOTE: créer render/compute/ComputeShaderSetup.java et ComputeShaderProvider.java (stubs vides)"

echo ""
echo "=== 12. Transitives oubliées (découvertes par review) ==="
cp -v "$SRC"/api/utils/ParseUtil.java "$DST/util/" 2>/dev/null
cp -v "$SRC"/api/utils/datastruct/*.java "$DST/util/datastruct/" 2>/dev/null
cp -v "$SRC"/api/exception/*.java "$DST/exception/" 2>/dev/null
cp -v "$SRC"/model/armature/HumanoidArmature.java "$DST/armature/" 2>/dev/null
cp -v "$SRC"/model/armature/types/HumanLikeArmature.java "$DST/armature/types/" 2>/dev/null
cp -v "$SRC"/client/mesh/HumanoidMesh.java "$DST/mesh/" 2>/dev/null

echo ""
echo "=== 13. ClientPlayerPatch + LocalPlayerPatch (typo upstream 'capabilites') ==="
cp -v "$SRC"/client/world/capabilites/entitypatch/player/AbstractClientPlayerPatch.java "$DST/patch/ClientPlayerPatch.java" 2>/dev/null
cp -v "$SRC"/client/world/capabilites/entitypatch/player/LocalPlayerPatch.java "$DST/patch/" 2>/dev/null

echo ""
echo "=== 14. Rewrite imports ==="
bash scripts/rig-rewrite-imports.sh "$DST"

echo ""
echo "=== 15. License headers ==="
bash scripts/rig-headers.sh "$DST"

echo ""
echo "--- Phase 0 extraction done ---"
echo ""
echo "Fichiers copiés :"
find "$DST" -type f -name "*.java" | wc -l
echo "LOC totales :"
find "$DST" -type f -name "*.java" -exec cat {} + | wc -l

echo ""
echo "Next steps (manuel, cf. EXTRACTION.md §9) :"
echo "  1. Fixer compile errors (strip combat from types, stub refs EpicFightMod/ClientConfig/ClientEngine/SkillManager)"
echo "  2. Strip valeurs combat de LivingMotion/LivingMotions enum, ajouter valeurs TiedUp"
echo "  3. Extraire RenderEngine.Events → render/TiedUpRenderEngine.java"
echo "  4. Créer ComputeShader stubs (render/compute/ComputeShaderSetup.java + ComputeShaderProvider.java no-op)"
echo "  5. Adapter TiedUpCapabilities.java depuis EpicFightCapabilities.java (retirer combat caps)"
echo "  6. Écrire TiedUpCapabilityEvents.java from scratch (~50L : RegisterCapabilitiesEvent + AttachCapabilitiesEvent)"
echo "  7. Fork mixins (MixinEntity, MixinLivingEntity, MixinLivingEntityRenderer — @Invoker only pour renderer)"
echo "  8. Simplifier LivingEntityPatch.java (1213L → ~400L, strip combat)"
echo "  9. Créer TiedUpRigConstants.java (factory ANIMATOR_PROVIDER)"
echo ""
echo "Budget total post-script : 2 à 3 semaines pour vert-compile."
