name: Build Platform Bindings

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  # Build and test the Rust library
  test:
    name: Test Rust Library
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Run tests
        run: cargo test --all-features

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

      - name: Run clippy
        run: cargo clippy -- -D warnings

  # Build Swift bindings and XCFramework
  build-swift:
    name: Build Swift Package
    runs-on: macos-latest
    needs: test
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-apple-darwin,x86_64-apple-darwin,aarch64-apple-ios,aarch64-apple-ios-sim,x86_64-apple-ios

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-swift-${{ hashFiles('**/Cargo.lock') }}

      - name: Build for all Apple platforms
        run: ./scripts/build-swift.sh --all

      - name: Upload Swift Package
        uses: actions/upload-artifact@v4
        with:
          name: swift-package
          path: bindings/swift/
          retention-days: 7

  # Build Kotlin/Android bindings
  build-kotlin:
    name: Build Kotlin/Android Package
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-linux-android,armv7-linux-androideabi,x86_64-linux-android,i686-linux-android

      - name: Setup Android NDK
        uses: nttld/setup-ndk@v1
        id: setup-ndk
        with:
          ndk-version: r26b
          add-to-path: true

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-kotlin-${{ hashFiles('**/Cargo.lock') }}

      - name: Install cargo-ndk
        run: command -v cargo-ndk || cargo install cargo-ndk --locked

      - name: Build for all Android architectures
        env:
          ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
        run: ./scripts/build-kotlin.sh --all

      - name: Upload Kotlin Package
        uses: actions/upload-artifact@v4
        with:
          name: kotlin-package
          path: bindings/kotlin/
          retention-days: 7

  # Generate bindings only (for quick validation)
  generate-bindings:
    name: Generate Bindings Only
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v4

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

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-bindings-${{ hashFiles('**/Cargo.lock') }}

      - name: Generate Swift bindings
        run: ./scripts/build-swift.sh --generate-only

      - name: Generate Kotlin bindings
        run: ./scripts/build-kotlin.sh --generate-only

      - name: Verify bindings generated
        run: |
          test -f bindings/swift/Sources/CooklangFind/CooklangFind.swift
          test -f bindings/kotlin/org/cooklang/find/cooklang_find.kt

