name: Release

on:
  push:
    tags:
      - "v*.*.*"

jobs:
  create-release:
    name: Create release
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Build changelog
        id: github_changelog
        run: |
          changelog=$(git log $(git tag | tail -2 | head -1)..HEAD --no-merges --oneline)
          echo "changelog<<EOF" >> $GITHUB_OUTPUT
          echo "$changelog" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          body: ${{ steps.github_changelog.outputs.changelog }}
        env:
          GITHUB_TOKEN: ${{ secrets.GH_PAT }}

  build-package:
    name: Build binaries
    needs: create-release
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-22.04, windows-latest]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.11"

      - name: Build artifacts
        run: |
          python -m pip install -r requirements.txt
          python -m pip install pyinstaller==6.20.0
          cd src
          python build_package.py

      - name: Upload release artifacts
        uses: softprops/action-gh-release@v2
        with:
          files: ./dist/flaresolverr_*
        env:
          GITHUB_TOKEN: ${{ secrets.GH_PAT }}
