name: Build and Release

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    name: Build proxy binary
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            binary_name: fbi-proxy-linux-x64
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            binary_name: fbi-proxy-linux-arm64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            binary_name: fbi-proxy-windows-x64.exe
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            binary_name: fbi-proxy-windows-arm64.exe
          - os: macos-latest
            target: x86_64-apple-darwin
            binary_name: fbi-proxy-macos-x64
          - os: macos-latest
            target: aarch64-apple-darwin
            binary_name: fbi-proxy-macos-arm64

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross-compilation tools
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu

      - name: Build binary
        run: |
          cargo build --release --target ${{ matrix.target }}

      - name: Prepare binary
        shell: bash
        run: |
          mkdir -p release
          if [ "${{ matrix.os }}" = "windows-latest" ]; then
            cp target/${{ matrix.target }}/release/fbi-proxy.exe release/${{ matrix.binary_name }}
          else
            cp target/${{ matrix.target }}/release/fbi-proxy release/${{ matrix.binary_name }}
            chmod +x release/${{ matrix.binary_name }}
          fi

      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.binary_name }}
          path: release/${{ matrix.binary_name }}

  release:
    name: Semantic Release
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main' && github.event_name == 'push' && github.repository_owner == 'snomiao'
    permissions:
      contents: write
      id-token: write
      issues: write
      pull-requests: write
      packages: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
      - uses: actions/setup-node@v6
        with:
          node-version: latest
          # Note: Do NOT set registry-url when using semantic-release
          # It creates an .npmrc that conflicts with semantic-release auth
      - uses: oven-sh/setup-bun@v2
      - name: Ensure npm 11.5.1+ for OIDC support
        run: npm install -g npm@latest
      - run: bun install
      # dont build rs here
      - run: bun run build:ts
      - run: bun run build:js

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: release

      - name: Flatten artifacts
        run: |
          mkdir -p release-flat
          find release -type f -exec cp {} release-flat/ \;
          ls -la release-flat/
          rm -rf release
          mv release-flat release

      - name: Set execute permissions for binaries
        run: |
          chmod +x release/fbi-proxy-linux-x64 || true
          chmod +x release/fbi-proxy-linux-arm64 || true
          chmod +x release/fbi-proxy-macos-x64 || true
          chmod +x release/fbi-proxy-macos-arm64 || true
          ls -la release/

      # IMPORTANT: For npm OIDC to work, you must configure Trusted Publisher at:
      # https://www.npmjs.com/package/fbi-proxy/settings
      # Provider: GitHub Actions, Org: snomiao, Repo: fbi-proxy, Workflow: release.yml
      - name: Run semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: bunx semantic-release
