name: Build and Lint

on:
  push:
    branches:
      - '**'
  pull_request:

jobs:
  lint:
    name: Lint with flake8
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flake8
      - name: Lint
        run: |
          # Stop the build if there are Python syntax errors or undefined names
          flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --builtins="_"
          # Exit if any flake8 issue is found (errors or warnings)
          flake8 . --count --max-complexity=10 --max-line-length=88 --statistics

  translation-check:
    name: Check translation template
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install meson ninja
          sudo apt-get update
          sudo apt-get install -y gettext blueprint-compiler desktop-file-utils libglib2.0-dev
      - name: Setup build directory
        run: |
          meson setup builddir
      - name: Check if .pot file needs updating
        run: |
          # Backup the original .pot file from the checkout
          cp po/drum-machine.pot po/drum-machine.pot.orig

          # Regenerate the .pot file
          meson compile -C builddir drum-machine-pot

          # Verify the generated .pot file exists
          if [ ! -f po/drum-machine.pot ]; then
            echo "::error::Failed to generate .pot file. Build may have failed."
            exit 1
          fi
          
          # Normalize both files by:
          # 1. Replacing timestamp with placeholder
          # 2. Removing line number references (e.g., ":123" -> "")
          normalize_pot() {
            sed -E \
              -e 's/POT-Creation-Date: [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}\+[0-9]{4}/POT-Creation-Date: TIMESTAMP/g' \
              -e '/^#:/ s/:[0-9]+//g' \
              "$1"
          }
          
          normalize_pot po/drum-machine.pot.orig > po/drum-machine.pot.orig.normalized
          normalize_pot po/drum-machine.pot > po/drum-machine.pot.normalized
          
          # Compare normalized files (ignore whitespace differences)
          if ! diff -u --ignore-all-space po/drum-machine.pot.orig.normalized po/drum-machine.pot.normalized > /dev/null; then
            echo "::error file=po/drum-machine.pot::Translation template (.pot file) is out of date. Please run 'meson compile -C builddir drum-machine-pot' and commit the updated po/drum-machine.pot file."
            echo ""
            echo "Diff of changes needed:"
            diff -u po/drum-machine.pot.orig.normalized po/drum-machine.pot.normalized || true
            exit 1
          fi
          
          echo "✓ Translation template is up to date"

  flatpak:
    name: Flatpak Builder
    runs-on: ubuntu-latest
    container:
      image: bilelmoussaoui/flatpak-github-actions:gnome-nightly
      options: --privileged
    steps:
      - uses: actions/checkout@v4
      - uses: flatpak/flatpak-github-actions/flatpak-builder@v6
        with:
          bundle: "drum-machine-devel.flatpak"
          manifest-path: "io.github.revisto.drum-machine.json"
          run-tests: "true"
          cache-key: flatpak-builder-${{ github.sha }}
