name: Build and Release

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    container:
      image: fedora:latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up C++ environment
        run: |
          dnf install -y gcc gcc-c++ cmake make

      - name: Install SDL2 dependencies
        run: |
          dnf install -y SDL2 SDL2-devel SDL2_image SDL2_image-devel SDL2_ttf SDL2_ttf-devel SDL2_net SDL2_net-devel SDL2_mixer SDL2_mixer-devel

      - name: Clean build directory
        run: rm -rf build

      - name: Create build directory
        run: mkdir build

      - name: Configure CMake
        run: cmake -S . -B build

      - name: Build project
        run: cmake --build build -- -j $(nproc)

      - name: Archive build artifacts
        uses: actions/upload-artifact@v3
        with:
          name: build-artifacts
          path: build/

  release:
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v3
        with:
          name: build-artifacts
          path: build/

      - name: Generate release tag
        id: generate_release_tag
        uses: amitsingh-007/next-release-tag@v6.0.0
        with:
          github_token: ${{ secrets.GH_TOKEN }}
          tag_prefix: 'v'
          tag_template: 'yyyy.mm.dd.i'

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          name: Release ${{ steps.generate_release_tag.outputs.next_release_tag }}
          tag_name: ${{ steps.generate_release_tag.outputs.next_release_tag }}
          files: build/bloubloulespoissons
          generate_release_notes: true
          token: ${{secrets.GH_TOKEN}}
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}