name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  # ============================================================================
  # BUILD LINUX (Nativo)
  # ============================================================================
  build-linux:
    runs-on: ubuntu-latest
    container: node:20-slim
    steps:
      - name: Checkout
      - uses: actions/checkout@v4

      - name: Instalar dependencias
        run: |
          apt-get update && apt-get install -y \
            build-essential cmake git pkg-config libsdl3-dev \
            libgl1-mesa-dev libx11-dev libxext-dev libxrandr-dev libxcursor-dev \
            libxi-dev libxinerama-dev libxxf86vm-dev libxss-dev \
            libasound2-dev libpulse-dev libudev-dev libwayland-dev libxkbcommon-dev

      #- name: Setup SDL3
      #  uses: libsdl-org/setup-sdl@main
      #  with:
      #    version: 3.2.26
      #    install-linux-dependencies: true

      - name: Compilar (Make Linux)
        run: |
          export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
          make linux_release

      - name: Subir artefacto
        uses: actions/upload-artifact@v4
        with:
          name: linux-build
          path: ./*-linux.tar.gz

  # ============================================================================
  # BUILD WINDOWS (Cross-Compile usando el nuevo target del Makefile)
  # ============================================================================
  build-windows:
    runs-on: ubuntu-latest
    container: node:20-slim
    steps:
      - uses: actions/checkout@v4

      - name: Instalar MinGW y Zip
        run: apt-get update && apt-get install -y build-essential wget mingw-w64 zip

      - name: Setup SDL3 (MinGW)
        run: |
          SDL_VER="3.2.26"
          wget -q https://github.com/libsdl-org/SDL/releases/download/release-${SDL_VER}/SDL3-devel-${SDL_VER}-mingw.tar.gz
          tar -xzf SDL3-devel-${SDL_VER}-mingw.tar.gz
          cp -r SDL3-${SDL_VER}/x86_64-w64-mingw32/* /usr/x86_64-w64-mingw32/

      - name: Compilar (Make Windows Cross)
        # AQUÍ ESTÁ LA MAGIA: Pasamos la ruta de la DLL al makefile
        run: make windows_cross SDL_DLL_PATH=/usr/x86_64-w64-mingw32/bin/SDL3.dll

      - name: Subir artefacto
        uses: actions/upload-artifact@v4
        with:
          name: windows-build
          path: ./*-win32-x64.zip

  # ============================================================================
  # RELEASE
  # ============================================================================
  create-release:
    needs: [build-linux, build-windows]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Crear Release en Gitea
        uses: https://github.com/softprops/action-gh-release@v1
        with:
          files: artifacts/*
          token: ${{ secrets.GITHUB_TOKEN }}