name: Build RSS feeds

on:
  schedule:
    - cron: "0 6 * * *" # daily at 06:00 UTC

  workflow_dispatch:
    inputs:
      verbose:
        description: "Enable verbose logging"
        required: false
        default: "false"
        type: boolean
      forceGenerationBernAsianHornetSightings:
        description: "force feed generation of asian hornets sightings in Bern"
        required: false
        default: "false"
        type: boolean
      forceGenerationBernPlacesOfWorship:
        description: "force feed generation of places of worship per religion in Bern"
        required: false
        default: "false"
        type: boolean
      ignoreFederalPopularInitiativesFeed:
        description: "skip federal initiatives feed generation"
        required: false
        default: "false"
        type: boolean

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.14"

      - name: Install package
        run: |
          python -m pip install --upgrade pip
          pip install .

      - name: Configure git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Preparing ENV variables
        run: |
          CACHE="$GITHUB_WORKSPACE/.cache"
          ASSETS="$GITHUB_WORKSPACE/assets"
          
          mkdir -p "$CACHE" "$ASSETS"
          
          echo "RESSPUBLICA_CACHE=$CACHE" >> $GITHUB_ENV
          echo "RESSPUBLICA_ASSETS=$ASSETS" >> $GITHUB_ENV

      - name: Generate feeds for federal popular initiatives
        run: |
          if ! [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.ignoreFederalPopularInitiativesFeed }}" = "true"]; then
            if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.verbose }}" = "true" ]; then
              echo "Running in VERBOSE mode"
              resspublica --verbose --gen_federalInitiatives
            else
              resspublica --gen_federalInitiatives
            fi
            git add feed/ .cache/
            
            if git diff --cached --quiet; then
              echo "No changes to commit"
              exit 0
            fi
            git commit -m "feeds: federal popular initiatives update $(date -u +%Y-%m-%d)"
          fi
      - name: Generate feeds for asian hornets sighting in Bern
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.verbose }}" = "true" ]; then
            echo "Running in VERBOSE mode"
            if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.forceGenerationBernAsianHornetSightings }}" = "true" ]; then
              resspublica --force_gen_bernAsianHornets --verbose
            else
              resspublica --gen_bernAsianHornets --verbose
            fi
          else
            if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.forceGenerationBernAsianHornetSightings }}" = "true" ]; then
              resspublica --force_gen_bernAsianHornets
            else
              resspublica --gen_bernAsianHornets
            fi
          fi
          git add feed/ .cache/ assets/
          
          if git diff --cached --quiet; then
            echo "No changes to commit"
            exit 0
          fi
          git commit -m "feeds: asian hornets sighting in Bern update $(date -u +%Y-%m-%d)"

      - name: Generate feeds for places of worship per religion in Bern
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.verbose }}" = "true" ]; then
            echo "Running in VERBOSE mode"
            if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.forceGenerationBernPlacesOfWorship }}" = "true" ]; then
              resspublica --force_gen_bernReligionMap --verbose
            else
              resspublica --gen_bernReligionMap --verbose
            fi
          else
            if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ "${{ inputs.forceGenerationBernPlacesOfWorship }}" = "true" ]; then
              resspublica --force_gen_bernReligionMap
            else
              resspublica --gen_bernReligionMap
            fi
          fi
          git add feed/ .cache/ assets/
          
          if git diff --cached --quiet; then
            echo "No changes to commit"
            exit 0
          fi
          git commit -m "feeds: places of worship per Religion in Bern update $(date -u +%Y-%m-%d)"


      - name: Push changes
        run: |
          git push origin main
