name: Build Linux .deb Package

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build-deb:
    name: Linux .deb (amd64)
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup Python 3.12
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Install build tools
        run: |
          sudo apt-get update
          sudo apt-get install -y dpkg-dev fakeroot
          pip install --upgrade pip setuptools wheel hatchling

      - name: Build Flutter web UI
        uses: subosito/flutter-action@v2
        with:
          flutter-version: '3.41.4'
          channel: stable
          cache: false

      - name: Flutter web build
        working-directory: flutter_app
        run: |
          flutter pub get
          flutter build web --release --no-tree-shake-icons
        continue-on-error: true

      - name: Build .deb package
        run: python installer/build_deb.py

      - name: Upload .deb artifact
        uses: actions/upload-artifact@v4
        with:
          name: cognithor-deb
          path: installer/dist/*.deb

      - name: Upload to GitHub Release
        if: startsWith(github.ref, 'refs/tags/')
        uses: softprops/action-gh-release@v2
        with:
          files: installer/dist/*.deb
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
