name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.name }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Linux (x86_64)
            target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
            asset_name: ironpad-linux-x86_64
          - name: macOS (x86_64)
            target: x86_64-apple-darwin
            os: macos-latest
            archive: tar.gz
            asset_name: ironpad-macos-x86_64
          - name: Windows (x86_64)
            target: x86_64-pc-windows-msvc
            os: windows-latest
            archive: zip
            asset_name: ironpad-windows-x86_64

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

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

      - name: Install system dependencies (Linux)
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y cmake libdbus-1-dev pkg-config

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Build frontend
        working-directory: frontend
        run: |
          npm ci
          npm run build

      - name: Copy frontend to static
        shell: bash
        run: |
          mkdir -p backend/static
          cp -r frontend/dist/* backend/static/

      - name: Build backend (release)
        working-directory: backend
        run: cargo build --release --target ${{ matrix.target }}

      - name: Package (Unix)
        if: matrix.archive == 'tar.gz'
        shell: bash
        run: |
          RELEASE_DIR="${{ matrix.asset_name }}-${{ github.ref_name }}"
          mkdir -p "$RELEASE_DIR"
          cp "backend/target/${{ matrix.target }}/release/ironpad" "$RELEASE_DIR/"
          cp -r backend/static "$RELEASE_DIR/static"
          cp README.md LICENSE "$RELEASE_DIR/"
          tar czf "$RELEASE_DIR.tar.gz" "$RELEASE_DIR"
          echo "ASSET=$RELEASE_DIR.tar.gz" >> $GITHUB_ENV

      - name: Package (Windows)
        if: matrix.archive == 'zip'
        shell: bash
        run: |
          RELEASE_DIR="${{ matrix.asset_name }}-${{ github.ref_name }}"
          mkdir -p "$RELEASE_DIR"
          cp "backend/target/${{ matrix.target }}/release/ironpad.exe" "$RELEASE_DIR/"
          cp -r backend/static "$RELEASE_DIR/static"
          cp README.md LICENSE "$RELEASE_DIR/"
          7z a "$RELEASE_DIR.zip" "$RELEASE_DIR"
          echo "ASSET=$RELEASE_DIR.zip" >> $GITHUB_ENV

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ env.ASSET }}

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          name: Ironpad ${{ github.ref_name }}
          body: |
            ## Downloads

            | Platform | File |
            |----------|------|
            | Windows (x86_64) | `ironpad-windows-x86_64-${{ github.ref_name }}.zip` |
            | macOS (x86_64) | `ironpad-macos-x86_64-${{ github.ref_name }}.tar.gz` |
            | Linux (x86_64) | `ironpad-linux-x86_64-${{ github.ref_name }}.tar.gz` |

            **Linux:** Extract the tar.gz and run `./ironpad`. Works on any distro -- it's a standalone binary with no dependencies.

            **macOS:** Extract and run. You may need to allow it in System Settings > Privacy & Security on first launch.

            **Windows:** Extract the zip and run `ironpad.exe`.

            ---
          files: artifacts/**/*
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
