name: Build

on:
  push:
    branches: [main]
  workflow_dispatch:

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-latest
            label: macOS
            build_cmd: npm run build:mac
            artifact_path: desktop-dist/tono.dmg
            artifact_name: tono-macos

          - os: ubuntu-latest
            label: Linux
            build_cmd: npm run build:linux
            artifact_path: desktop-dist/tono-linux.tar.gz
            artifact_name: tono-linux

          - os: windows-latest
            label: Windows
            build_cmd: npm run build:windows
            artifact_path: desktop-dist
            artifact_name: tono-windows

    runs-on: ${{ matrix.os }}
    name: Build (${{ matrix.label }})

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip

      - uses: actions/setup-node@v4
        with:
          node-version: "24"
          cache: npm

      - name: Install uv
        run: pip install uv

      - name: Create venv
        run: uv venv --python 3.13

      # Linux: pywebview needs webkit2gtk headers to import during PyInstaller collection
      - name: Install Linux system dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update -q
          sudo apt-get install -y \
            python3-gi python3-gi-cairo \
            gir1.2-gtk-3.0 \
            libwebkit2gtk-4.1-dev \
            gir1.2-webkit2-4.1

      - name: Install Node dependencies
        run: npm install --ignore-scripts

      - name: Install frontend Node dependencies
        working-directory: frontend
        run: npm install

      - name: Build
        run: ${{ matrix.build_cmd }}
        shell: bash

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact_name }}
          path: ${{ matrix.artifact_path }}
          if-no-files-found: error
