name: Release (Linux, macOS, Windows)
permissions:
  contents: write

on:
  workflow_dispatch:
    inputs:
      name:
        description: 'reason'
        required: false
  push:
    tags:
      - '*'
      - '!*-alpha*'

jobs:
  linux:
    name: Linux Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Determine Version
        run: |
          VERSION=$(git describe --tags)
          echo "VERSION=$VERSION" >> $GITHUB_ENV
      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest
      - name: Build Frontend
        env:
          CI: ""
        run: |
          cd web
          bun install
          DISABLE_ESLINT_PLUGIN='true' VITE_REACT_APP_VERSION=$VERSION bun run build
          cd ..
      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: '>=1.25.1'
      - name: Build Backend (amd64)
        run: |
          go mod download
          go build -ldflags "-s -w -X 'new-api/common.Version=$VERSION' -extldflags '-static'" -o new-api-$VERSION
      - name: Build Backend (arm64)
        run: |
          sudo apt-get update
          DEBIAN_FRONTEND=noninteractive sudo apt-get install -y gcc-aarch64-linux-gnu
          CC=aarch64-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -ldflags "-s -w -X 'new-api/common.Version=$VERSION' -extldflags '-static'" -o new-api-arm64-$VERSION
      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: |
            new-api-*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  macos:
    name: macOS Release
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Determine Version
        run: |
          VERSION=$(git describe --tags)
          echo "VERSION=$VERSION" >> $GITHUB_ENV
      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest
      - name: Build Frontend
        env:
          CI: ""
          NODE_OPTIONS: "--max-old-space-size=4096"
        run: |
          cd web
          bun install
          DISABLE_ESLINT_PLUGIN='true' VITE_REACT_APP_VERSION=$VERSION bun run build
          cd ..
      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: '>=1.25.1'
      - name: Build Backend
        run: |
          go mod download
          go build -ldflags "-X 'new-api/common.Version=$VERSION'" -o new-api-macos-$VERSION
      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: new-api-macos-*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  windows:
    name: Windows Release
    runs-on: windows-latest
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Determine Version
        run: |
          VERSION=$(git describe --tags)
          echo "VERSION=$VERSION" >> $GITHUB_ENV
      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest
      - name: Build Frontend
        env:
          CI: ""
        run: |
          cd web
          bun install
          DISABLE_ESLINT_PLUGIN='true' VITE_REACT_APP_VERSION=$VERSION bun run build
          cd ..
      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: '>=1.25.1'
      - name: Build Backend
        run: |
          go mod download
          go build -ldflags "-s -w -X 'new-api/common.Version=$VERSION'" -o new-api-$VERSION.exe
      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: new-api-*.exe
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
