name: Draft Release

on:
  push:
    branches:
      - main

permissions:
  contents: write
  pull-requests: write

jobs:
  draft-release:
    name: Draft Release
    runs-on: ubuntu-slim
    outputs:
      release-id: ${{ steps.drafter.outputs.id }}
      tag-name: ${{ steps.drafter.outputs.tag_name }}
      version: ${{ steps.drafter.outputs.resolved_version }}
    steps:
      - name: Draft release
        id: drafter
        uses: release-drafter/release-drafter@v6
        with:
          disable-autolabeler: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build-web:
    name: Build Web App
    needs: draft-release
    if: needs.draft-release.outputs.tag-name != '' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest

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

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version-file: ".node-version"
          cache: "npm"

      - name: Update package.json version
        run: |
          npm version ${{ needs.draft-release.outputs.version }} --no-git-tag-version

      - name: Install dependencies
        run: npm ci

      - name: Build web app
        run: npm run web:build -- --base=/respira/

      - name: Create web artifact zip
        run: |
          cd dist
          zip -r ../respira-web-${{ needs.draft-release.outputs.version }}.zip .

      - name: Upload web artifact
        uses: actions/upload-artifact@v4
        with:
          name: web-artifacts
          path: respira-web-${{ needs.draft-release.outputs.version }}.zip

  build-release:
    name: Build Release - ${{ matrix.os }}
    needs: draft-release
    if: needs.draft-release.outputs.tag-name != '' && github.ref == 'refs/heads/main'
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]

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

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version-file: ".node-version"
          cache: "npm"

      - name: Update package.json version
        run: |
          npm version ${{ needs.draft-release.outputs.version }} --no-git-tag-version

      - name: Install dependencies
        run: npm ci

      - name: Package and make
        run: npm run electron:make

      - name: Upload Windows artifacts
        if: matrix.os == 'windows-latest'
        uses: actions/upload-artifact@v4
        with:
          name: windows-artifacts
          path: out/make/squirrel.windows/x64/*

      - name: Upload macOS artifacts
        if: matrix.os == 'macos-latest'
        uses: actions/upload-artifact@v4
        with:
          name: macos-artifacts
          path: |
            out/make/zip/darwin/**/*.zip
            out/make/zip/darwin/**/*RELEASES.json
            out/make/*.dmg

      - name: Upload Linux artifacts
        if: matrix.os == 'ubuntu-latest'
        uses: actions/upload-artifact@v4
        with:
          name: linux-artifacts
          path: |
            out/make/rpm/x64/*.rpm
            out/make/deb/x64/*.deb
            out/make/AppImage/x64/*.AppImage

  upload-to-release:
    name: Upload to GitHub Release
    needs: [draft-release, build-web, build-release]
    if: needs.draft-release.outputs.tag-name != '' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-slim

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

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

      - name: Delete existing release artifacts
        run: |
          gh release view ${{ needs.draft-release.outputs.tag-name }} --json assets --jq '.assets[].name' | \
          xargs -I {} gh release delete-asset ${{ needs.draft-release.outputs.tag-name }} {} --yes || true
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload all assets to release
        run: |
          find artifacts -type f -exec gh release upload ${{ needs.draft-release.outputs.tag-name }} {} --clobber \;
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
