name: Build and Upload Canary
on:
  push:
    tags:
      - "v*.*.*"
  workflow_dispatch:

jobs:
  create-draft:
    runs-on: ubuntu-latest
    outputs:
      current-version: ${{ steps.package-version.outputs.current-version }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Get Version
        id: package-version
        uses: martinbeentjes/npm-get-version-action@v1.3.1

      - name: Get Changelog Entry
        id: changelog_reader
        uses: artlaman/conventional-changelog-reader-action@v1.1.0
        with:
          version: ${{ steps.package-version.outputs.current-version }}
          path: ./CHANGELOG.md

      - name: Create release draft
        uses: ncipollo/release-action@v1
        with:
          # This pulls from the "Get Changelog Entry" step above, referencing it's ID to get its outputs object.
          # See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
          draft: true
          tag: ${{ steps.changelog_reader.outputs.version }}
          name: Release ${{ steps.changelog_reader.outputs.version }}
          body: ${{ steps.changelog_reader.outputs.changes }}
          prerelease: ${{ steps.changelog_reader.outputs.status == 'prereleased' }}
          allowUpdates: true
          token: ${{ secrets.GITHUB_TOKEN }}
  build:
    needs: create-draft
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [windows-latest, ubuntu-latest]
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          lfs: true

      - name: Bun Install
        uses: oven-sh/setup-bun@v2

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Build Canary
        run: bun run package:${{ runner.os }}
        env:
          BUILD_DIR: ./build/${{ runner.os }}
          TWITCH_CLIENT_ID: ${{ secrets.TWITCH_CLIENT_ID }}

      - name: Install 7zip (minimal)
        if: matrix.os == 'ubuntu-latest'
        run: sudo apt-get update && sudo apt-get install -y p7zip-full

      - name: Upload Linux Artifact
        uses: actions/upload-artifact@v4
        with:
          name: canary-build-${{ runner.os }}
          path: build/${{ runner.os }}
  release:
    needs: [build, create-draft]
    runs-on: ubuntu-latest

    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v5

      - name: Archive Windows Release
        uses: thedoctor0/zip-release@0.7.5
        with:
          type: "zip"
          directory: ${{ github.workspace }}
          filename: "Gameflow-Windows.zip"
          path: "canary-build-Windows"

      - name: Publish Release
        uses: ncipollo/release-action@v1
        with:
          draft: false
          allowUpdates: true
          tag: ${{ needs.create-draft.outputs.current-version }}
          omitNameDuringUpdate: true
          omitBodyDuringUpdate: true
          replacesArtifacts: true
          token: ${{ secrets.GITHUB_TOKEN }}
          artifacts: "${{ github.workspace }}/canary-build-*/*.AppImage,${{ github.workspace }}/Gameflow-Windows.zip"
