name: Rust CI

on:
  push:
    branches: [main]
    paths:
      - 'extensions/underthesea_core/**'
      - '.github/workflows/ci-rust.yml'
  pull_request:
    branches: [main]
    paths:
      - 'extensions/underthesea_core/**'
      - '.github/workflows/ci-rust.yml'

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Rust Tests
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./extensions/underthesea_core

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            extensions/underthesea_core/target/
          key: ${{ runner.os }}-cargo-v2-${{ hashFiles('extensions/underthesea_core/Cargo.toml') }}
          restore-keys: |
            ${{ runner.os }}-cargo-v2-

      - name: Check formatting
        run: cargo fmt --all -- --check

      - name: Run clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Run tests
        run: cargo test --verbose

  build-python:
    name: Build Python Extension
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./extensions/underthesea_core

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            extensions/underthesea_core/target/
          key: ${{ runner.os }}-cargo-build-v2-${{ hashFiles('extensions/underthesea_core/Cargo.toml') }}
          restore-keys: |
            ${{ runner.os }}-cargo-build-v2-

      - name: Install maturin
        run: pip install maturin

      - name: Verify dynamic version in pyproject.toml
        run: |
          if ! grep -q 'dynamic = \["version"\]' pyproject.toml; then
            echo "ERROR: pyproject.toml must use dynamic version from Cargo.toml"
            echo "Add 'dynamic = [\"version\"]' to [project] section and remove hardcoded version"
            exit 1
          fi
          echo "✓ pyproject.toml uses dynamic version from Cargo.toml"

      - name: Clean old wheels
        run: rm -rf target/wheels/

      - name: Build Python extension
        run: maturin build --release

      - name: Verify wheel version matches Cargo.toml
        run: |
          CARGO_VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
          WHEEL_FILE=$(ls target/wheels/*.whl | head -1)
          WHEEL_VERSION=$(basename "$WHEEL_FILE" | sed 's/underthesea_core-\([^-]*\)-.*/\1/')
          echo "Cargo.toml version: $CARGO_VERSION"
          echo "Wheel version: $WHEEL_VERSION"
          if [ "$CARGO_VERSION" != "$WHEEL_VERSION" ]; then
            echo "ERROR: Version mismatch! Cargo.toml=$CARGO_VERSION, Wheel=$WHEEL_VERSION"
            exit 1
          fi
          echo "✓ Versions are consistent"

      - name: Test Python import
        run: |
          pip install target/wheels/*.whl
          python -c "from underthesea_core import LRTrainer, LRClassifier, LRModel; print('LR module imported successfully')"
          python -c "from underthesea_core import CRFTrainer, CRFTagger, CRFModel; print('CRF module imported successfully')"
          python -c "from underthesea_core import TfIdfVectorizer; print('TfIdfVectorizer module imported successfully')"
