name: Build

permissions:
  contents: write

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          target: |
            x86_64-unknown-linux-gnu

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/target/
          key: test-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Test
        run: cargo test

  build-arm:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          target: |
            aarch64-unknown-linux-gnu

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/target/
          key: arm-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Install Zig
        uses: mlugg/setup-zig@v2
        with:
          version: 0.13.0

      - name: Install cargo-zigbuild
        run: cargo install cargo-zigbuild || true

      - name: Build ARM
        run: cargo zigbuild --release --target aarch64-unknown-linux-gnu

      - name: Upload ARM artifact
        uses: actions/upload-artifact@v4
        with:
          name: jwkserve-arm
          path: target/aarch64-unknown-linux-gnu/release/jwkserve

  build-x86:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          target: |
            x86_64-unknown-linux-gnu

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/target/
          key: x86-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Install Zig
        uses: mlugg/setup-zig@v2
        with:
          version: 0.13.0

      - name: Install cargo-zigbuild
        run: cargo install cargo-zigbuild || true

      - name: Build X86
        run: cargo zigbuild --release --target x86_64-unknown-linux-gnu

      - name: Upload X86 artifact
        uses: actions/upload-artifact@v4
        with:
          name: jwkserve-x86
          path: target/x86_64-unknown-linux-gnu/release/jwkserve

  release:
    runs-on: ubuntu-latest
    needs: [test, build-arm, build-x86]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.RELEASE_TOKEN }}

      - name: Semantic Release
        uses: cycjimmy/semantic-release-action@v5
        with:
          extra_plugins: |
            @semantic-release/git
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}