name: Build & Release

on:
  push:
    branches: [main]
    tags: ['v*']
  workflow_dispatch:

jobs:
  build-linux:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          channel: stable
          cache: true

      - name: Install Linux dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev ninja-build clang cmake pkg-config patchelf fakeroot dpkg

      - name: Get dependencies
        run: flutter pub get

      - name: Analyze
        run: flutter analyze --no-fatal-infos

      - name: Test
        run: flutter test

      - name: Build Linux release
        run: flutter build linux --release

      - name: Build .deb package
        run: bash dist/rebuild-deb.sh

      - name: Upload .deb artifact
        uses: actions/upload-artifact@v4
        with:
          name: imajviewer-linux
          path: dist/imajviewer_*.deb

  build-windows:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          channel: stable
          cache: true

      - name: Get dependencies
        run: flutter pub get

      - name: Build Windows release
        run: flutter build windows --release

      - name: Install Inno Setup
        run: |
          choco install innosetup --no-progress -y

      - name: Sync version from pubspec.yaml
        shell: pwsh
        run: |
          $version = (Select-String -Path pubspec.yaml -Pattern '^version:\s*([0-9]+\.[0-9]+\.[0-9]+)').Matches[0].Groups[1].Value
          (Get-Content windows/installer.iss -Raw) -replace '#define MyAppVersion "[^"]+"', "#define MyAppVersion `"$version`"" | Set-Content windows/installer.iss
          Write-Host "Inno Setup version: $version"

      - name: Build Inno Setup installer
        shell: cmd
        run: |
          "C:\ProgramData\chocolatey\bin\ISCC.exe" windows\installer.iss

      - name: Upload installer artifact
        uses: actions/upload-artifact@v4
        with:
          name: imajviewer-windows
          path: |
            build/windows/x64/runner/Release/
            dist/imajviewer_setup_*.exe

  create-release:
    needs: [build-linux, build-windows]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: Download Linux artifacts
        uses: actions/download-artifact@v4
        with:
          name: imajviewer-linux
          path: release-artifacts

      - name: Download Windows artifacts
        uses: actions/download-artifact@v4
        with:
          name: imajviewer-windows
          path: release-artifacts/windows

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          overwrite_files: true
          name: "imajViewer ${{ github.ref_name }}"
          body: |
            imajViewer ${{ github.ref_name }}

            See the [changelog](CHANGELOG.md) for details.

            ### Downloads
            - Linux: `.deb` package (Ubuntu/Debian)
            - Windows: Inno Setup installer
          files: |
            release-artifacts/imajviewer_*.deb
            release-artifacts/windows/dist/imajviewer_setup_*.exe
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
