name: Build and Release

on:
  workflow_run:
    workflows: ["Test with Pytest"]
    types:
      - completed

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        arch: [x86_64, arm64]

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Set up Python (x86_64)
        if: matrix.arch == 'x86_64'
        uses: actions/setup-python@v6
        with:
          python-version: '3.14'

#      - name: Install QEMU (arm)
#        if: matrix.arch == 'arm64'
#        uses: docker/setup-qemu-action@v3
#        with:
#          platforms: arm64

      - name: Set up Docker Buildx (arm)
        if: matrix.arch == 'arm64'
        uses: docker/setup-buildx-action@v3
        with:
          platforms: linux/arm64

      - name: Build Executable
        run: |
          if [ "${{ matrix.arch }}" = "x86_64" ]; then
            pip install -r requirements.txt
            cd app
            pyinstaller --onefile --add-data "ui:ui" --name wolServer-${{ matrix.arch }} wolServer.py
          else
            echo 'FROM arm64v8/python:3.13-slim-bullseye AS builder
            WORKDIR /app
            COPY requirements.txt .
            COPY app .
            RUN apt-get update && apt-get install -y --no-install-recommends binutils \
                && pip install -r requirements.txt \
                && pyinstaller --onefile --add-data "ui:ui" --name wolServer-${{ matrix.arch }} wolServer.py \
                && mkdir -p /output && cp dist/wolServer-${{ matrix.arch }} /output/
            FROM scratch
            COPY --from=builder /output /output' > Dockerfile
            docker buildx build --platform linux/arm64/v8 --output type=local,dest=app/dist .
          fi

      - name: Upload Artifact
        uses: actions/upload-artifact@v3
        with:
          name: wolServer-${{ matrix.arch }}
          path: app/dist/**

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

    steps:
      - name: Download Artifacts
        uses: actions/download-artifact@v3
        with:
          path: app/dist

      - name: Generate Tag
        id: tag
        run: echo "TAG=v$(TZ='Europe/Athens' date +'%d%m%Y%H%M%S')" >> $GITEA_ENV

      - name: Create Release
        uses: akkuman/gitea-release-action@v1
        with:
          tag_name: "${{ env.TAG }}"
          name: "${{ env.TAG }}"
          target_commitish: main
          body: "Automated push release"
          token: ${{ secrets.mytoken }}
          files: |-
            /workspace/konsthol/WOL-Ly/app/dist/**

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

    steps:
      - name: Cleanup Buildx Containers
        run: |
          #docker images --filter "reference=moby/buildkit" -q | xargs -r docker rmi
          #docker images --filter "reference=tonistiigi/binfmt" -q | xargs -r docker rmi

          #docker images --filter "reference=gitea/runner-images" -q | xargs -r docker rmi
          #docker volume ls --filter "name=act" -q | xargs -r docker volume rm
