#!/bin/bash

# NNUE Training Pipeline (bash version)
# Works on Linux, macOS, and Windows (with Git Bash or WSL)

set -e  # Exit on error

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# Use python or python3 (check which is available)
PYTHON_CMD="python3"
if ! command -v python3 &> /dev/null; then
    PYTHON_CMD="python"
fi

echo "=== NNUE Training Pipeline ==="
echo ""
echo "Python command: $PYTHON_CMD"
echo "Working directory: $SCRIPT_DIR"
echo ""

# Step 1: Generate positions
echo "Step 1: Generating 500,000 random positions..."
$PYTHON_CMD generate_positions.py positions.txt
if [ ! -f positions.txt ]; then
    echo "ERROR: positions.txt not created"
    exit 1
fi
echo "✓ Positions generated"
echo ""

# Step 2: Label positions with Stockfish
echo "Step 2: Labeling positions with Stockfish (depth 12)..."
STOCKFISH_PATH="${STOCKFISH_PATH:-/usr/games/stockfish}"
echo "Using Stockfish: $STOCKFISH_PATH"
$PYTHON_CMD label_positions.py positions.txt training_data.jsonl "$STOCKFISH_PATH"
if [ ! -f training_data.jsonl ]; then
    echo "ERROR: training_data.jsonl not created"
    exit 1
fi
echo "✓ Positions labeled"
echo ""

# Step 3: Train NNUE model with versioning
echo "Step 3: Training NNUE model (20 epochs)..."

# Auto-detect latest version and increment
LATEST_VERSION=$(ls -1 nnue_weights_v*.pt 2>/dev/null | sed 's/nnue_weights_v//;s/.pt$//' | sort -n | tail -1)
NEW_VERSION=$((${LATEST_VERSION:-0} + 1))
WEIGHTS_FILE="nnue_weights_v${NEW_VERSION}.pt"

echo "Creating version v${NEW_VERSION}..."
$PYTHON_CMD train_nnue.py training_data.jsonl "$WEIGHTS_FILE"
if [ ! -f "$WEIGHTS_FILE" ]; then
    echo "ERROR: $WEIGHTS_FILE not created"
    exit 1
fi
echo "✓ Model trained: $WEIGHTS_FILE"
echo ""

# Step 4: Export weights to Scala
echo "Step 4: Exporting weights to Scala..."
SCALA_FILE="../src/main/scala/de/nowchess/bot/bots/nnue/NNUEWeights_v${NEW_VERSION}.scala"
$PYTHON_CMD export_weights.py "$WEIGHTS_FILE" "$SCALA_FILE"
if [ ! -f "$SCALA_FILE" ]; then
    echo "ERROR: $SCALA_FILE not created"
    exit 1
fi
echo "✓ Weights exported: $SCALA_FILE"
echo ""

echo "=== Pipeline Complete ==="
echo ""
echo "Next steps:"
echo "1. Navigate to project root: cd ../.."
echo "2. Compile: ./compile"
echo "3. Test: ./test"
