name: Build and Release Packages

on:
  push:
    tags:
      - 'v*'

jobs:
  build-windows:
    runs-on: windows-latest
    outputs:
      artifact-path: ${{ steps.upload.outputs.artifact-path }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install NSIS
        run: choco install nsis --no-progress -y

      - name: Setup MSVC
        uses: microsoft/setup-msbuild@v2

      - name: Configure project
        run: cmake -B build -S . -A x64

      - name: Build project
        run: cmake --build build --config Release

      - name: Generate NSIS installer
        run: cd build && cpack -G NSIS

      - name: Upload installer
        id: upload
        uses: actions/upload-artifact@v6
        with:
          name: windows-installer
          path: build/*.exe

  build-ubuntu:
    runs-on: ubuntu-latest
    outputs:
      artifact-path: ${{ steps.upload.outputs.artifact-path }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y cmake build-essential git \
            libglfw3-dev libfreetype6-dev libfontconfig1-dev \
            zlib1g-dev libharfbuzz-dev \
            libx11-dev libxrandr-dev libxi-dev libgtk-3-dev \
            mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev \
            libxinerama-dev libxcursor-dev libxkbcommon-dev

      - name: Configure CMake
        run: cmake -B build -DCMAKE_BUILD_TYPE=Release -DUSE_SYSTEM_GLFW=OFF

      - name: Build
        run: cmake --build build --config Release

      - name: Package DEB
        run: cd build && cpack -G DEB

      - name: Upload DEB
        id: upload
        uses: actions/upload-artifact@v6
        with:
          name: deb-package
          path: build/*.deb

  build-fedora:
    runs-on: ubuntu-latest
    container:
      image: fedora:latest
    outputs:
      artifact-path: ${{ steps.upload.outputs.artifact-path }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install dependencies
        run: |
          dnf install -y cmake gcc gcc-c++ make rpm-build git \
            glfw-devel freetype-devel fontconfig-devel \
            zlib-devel harfbuzz-devel \
            libX11-devel libXrandr-devel libXi-devel \
            mesa-libGL-devel mesa-libGLU-devel \
            libXinerama-devel libXcursor-devel \
            wayland-devel wayland-protocols-devel gtk3-devel

      - name: Configure CMake
        run: cmake -B build -DCMAKE_BUILD_TYPE=Release -DUSE_SYSTEM_GLFW=ON

      - name: Build
        run: cmake --build build --config Release

      - name: Package RPM
        run: cd build && cpack -G RPM

      - name: Upload RPM
        id: upload
        uses: actions/upload-artifact@v6
        with:
          name: rpm-package
          path: build/*.rpm

  release:
    needs: [build-windows, build-ubuntu, build-fedora]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download Windows artifact
        uses: actions/download-artifact@v6
        with:
          name: windows-installer
          path: artifacts/

      - name: Download DEB artifact
        uses: actions/download-artifact@v6
        with:
          name: deb-package
          path: artifacts/

      - name: Download RPM artifact
        uses: actions/download-artifact@v6
        with:
          name: rpm-package
          path: artifacts/

      - name: Create Release and Upload Assets
        uses: ncipollo/release-action@v1
        with:
          tag: ${{ github.ref_name }}
          name: Release ${{ github.ref_name }}
          artifacts: |
            artifacts/*.exe
            artifacts/*.deb
            artifacts/*.rpm