#!/bin/bash
set -e

echo "=================================================="
echo " OpenFOAM AI Case Generator"
echo "=================================================="
echo ""

# ============================================================================
# CHECK OLLAMA
# ============================================================================

if ! pgrep -x "ollama" > /dev/null; then
    echo "[ERROR] Ollama is not running."
    echo "Start Ollama using:"
    echo "    ollama serve"
    exit 1
fi

echo "[OK] Ollama service detected"
echo ""


# ============================================================================
# PREPARE OPENFOAM TUTORIALS
# ============================================================================

if [ ! -d "tutorials" ]; then

    echo "[INFO] tutorials directory not found"

    if [ -n "$FOAM_TUTORIALS" ] && [ -d "$FOAM_TUTORIALS" ]; then

        echo "[INFO] Copying OpenFOAM tutorials..."
        cp -r "$FOAM_TUTORIALS" tutorials

    elif [ -n "$WM_PROJECT_DIR" ] && [ -d "$WM_PROJECT_DIR/tutorials" ]; then

        echo "[INFO] Copying OpenFOAM tutorials..."
        cp -r "$WM_PROJECT_DIR/tutorials" tutorials

    else

        echo "[ERROR] OpenFOAM environment not detected"
        echo "[ERROR] Please source your OpenFOAM installation first"
        exit 1

    fi

    echo "[OK] tutorials directory created"
    echo ""

else

    echo "[OK] tutorials directory already exists"
    echo ""

fi



# ============================================================================
# CHECK TUTORIAL DATASET
# ============================================================================

if [ ! -d "Flattened_Tut" ]; then
    echo "[INFO] Flattened tutorial dataset not found"
    echo "[INFO] Running tutorial extraction pipeline..."
    
    python Tut_Creation.py
    
    echo "[OK] Flattened tutorial dataset generated"
    echo ""
else
    echo "[OK] Flattened tutorial dataset already exists"
    echo ""
fi

# ============================================================================
# CHECK STRUCTURE DATABASE
# ============================================================================

if [ ! -f "StructureDB.json" ]; then
    echo "[INFO] StructureDB not found"
    echo "[INFO] Building StructureDB..."
    
    python build_structure_db.py
    
    echo "[OK] StructureDB generated"
    echo ""
else
    echo "[OK] StructureDB already exists"
    echo ""
fi

# ============================================================================
# CHECK VECTOR DATABASES
# ============================================================================

VECTOR_DB_DIR="VectorDB"

REQUIRED_DBS=(
    "boundaryDB"
    "controlDictDB"
    "fvSchemesDB"
    "fvSolutionDB"
    "physicsDB"
    "geoDB"
)

MISSING_DB=false

for db in "${REQUIRED_DBS[@]}"; do
    if [ ! -d "$VECTOR_DB_DIR/$db" ]; then
        MISSING_DB=true
        break
    fi
done

if [ "$MISSING_DB" = true ]; then
    echo "[INFO] Vector databases not found"
    echo "[INFO] Generating FAISS vector databases..."
    
    python VectorDB.py
    
    echo "[OK] Vector databases generated"
    echo ""
else
    echo "[OK] Vector databases already exist"
    echo ""
fi

# ============================================================================
# VERIFY INPUT PROMPT
# ============================================================================

if [ ! -f "input/prompt" ]; then
    echo "[ERROR] input/prompt not found"
    exit 1
fi

if [ ! -s "input/prompt" ]; then
    echo "[ERROR] input/prompt is empty"
    exit 1
fi

echo "[OK] Input prompt detected"
echo ""

# ============================================================================
# CASE GENERATION
# ============================================================================

echo "=================================================="
echo " Starting Case Generation"
echo "=================================================="
echo ""

python scripts/run_case.py

echo ""
echo "[OK] Case generation completed"
echo ""

# ============================================================================
# OPTIONAL ERROR CORRECTION LOOP
# ============================================================================

echo "Do you want to start the automated debugging loop?"
echo "1) Yes"
echo "2) No"
echo ""

read -p "Enter choice (1-2): " debug_choice

case $debug_choice in

    1)
        echo ""
        echo "=================================================="
        echo " Starting Error Correction Pipeline"
        echo "=================================================="
        echo ""
        
        python scripts/run_error_loop.py
        
        echo ""
        echo "[OK] Error correction workflow completed"
        ;;
        
    2)
        echo ""
        echo "[INFO] Skipping error correction pipeline"
        ;;
        
    *)
        echo ""
        echo "[INFO] Invalid choice"
        echo "[INFO] Skipping error correction pipeline"
        ;;
esac

# ============================================================================
# FINAL SUMMARY
# ============================================================================

echo ""
echo "=================================================="
echo " Workflow Completed"
echo "=================================================="
echo ""

echo "Generated OpenFOAM case:"
echo "    Case/"
echo ""

echo "Next steps:"
echo "    cd Case"
echo "    simpleFoam"
echo ""

echo "Visualization:"
echo "    paraFoam -builtin"
echo ""

echo "=================================================="
echo " Done"
echo "=================================================="

