name: Build and Release

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

jobs:
  build-x86_64:
    runs-on: ubuntu-latest

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

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.14'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Build with PyInstaller
        run: |
          cd app
          pyinstaller --onefile --add-data "ui:ui" wolServer.py

      - name: Upload x86_64 Artifact
        uses: actions/upload-artifact@v4
        with:
          name: executable-x86_64
          path: app/dist/wolServer

  build-arm:
    runs-on: ubuntu-latest

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

      - name: Install QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build ARM Executable
        run: |
          docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
          docker buildx create --use
          docker buildx build --platform linux/arm64/v8 -t arm-builder --load --file - . <<EOF
          FROM arm64v8/python:3.13-slim-trixie
          WORKDIR /app
          COPY requirements.txt /app/requirements.txt
          COPY app /app/app
          ENV DEBIAN_FRONTEND=noninteractive
          ENV PIP_ROOT_USER_ACTION=ignore
          RUN apt-get update && apt-get install -y \
              build-essential \
              python3-dev \
              libffi-dev \
              libssl-dev \
              libsqlite3-dev \
              zlib1g-dev \
              && python -m pip install --upgrade pip \
              && pip install -r /app/requirements.txt \
              && cd /app/app \
              && pyinstaller --onefile --add-data "ui:ui" wolServer.py
          EOF

      - name: Create Dist Directory
        run: mkdir -p app/dist

      - name: Extract ARM Executable
        run: |
          container_id=$(docker create --platform linux/arm64/v8 arm-builder)
          docker cp $container_id:/app/app/dist/wolServer app/dist/wolServer
          docker rm $container_id

      - name: Upload ARM Artifact
        uses: actions/upload-artifact@v4
        with:
          name: executable-arm
          path: app/dist/wolServer

  release:
    needs: [build-x86_64, build-arm]
    runs-on: ubuntu-latest

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

      - name: Create Dist Directory
        run: mkdir -p app/dist

      - name: Download x86_64 Artifact
        uses: actions/download-artifact@v6
        with:
          name: executable-x86_64
          path: app/dist

      - name: Verify x86_64 Artifact
        run: ls -la app/dist

      - name: Rename x86_64 Artifact
        run: mv app/dist/wolServer app/dist/wolServer-x86_64

      - name: Download ARM Artifact
        uses: actions/download-artifact@v6
        with:
          name: executable-arm
          path: app/dist

      - name: Verify ARM Artifact
        run: ls -la app/dist

      - name: Rename ARM Artifact
        run: mv app/dist/wolServer app/dist/wolServer-arm

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

      - name: Create Release
        env:
          GITEA_TOKEN: ${{ secrets.mytoken }}
        run: |
          curl -X POST "https://git.konsthol.eu/api/v1/repos/konsthol/WOL-Ly/releases" \
          -H "Authorization: token ${{ secrets.mytoken }}" \
          -H "Content-Type: application/json" \
          -d '{
            "tag_name": "'"${{ env.TAG }}"'",
            "target_commitish": "main",
            "name": "'"${{ env.TAG }}"'",
            "body": "Automated push release",
            "draft": false,
            "prerelease": false
          }'

      - name: Upload x86_64 Executable to Release
        env:
          GITEA_TOKEN: ${{ secrets.mytoken }}
        run: |
          if [ -f "app/dist/wolServer-x86_64" ]; then
            curl --data-binary "@app/dist/wolServer-x86_64" \
            -H "Authorization: token ${{ secrets.mytoken }}" \
            -H "Content-Type: application/octet-stream" \
            "https://git.konsthol.eu/api/v1/repos/konsthol/WOL-Ly/releases/$(curl -s https://git.konsthol.eu/api/v1/repos/konsthol/WOL-Ly/releases/latest | jq -r '.id')/assets?name=wolServer-x86_64"
          else
            echo "x86_64 executable not found"
          fi

      - name: Upload ARM Executable to Release
        env:
          GITEA_TOKEN: ${{ secrets.mytoken }}
        run: |
          if [ -f "app/dist/wolServer-arm" ]; then
            curl --data-binary "@app/dist/wolServer-arm" \
            -H "Authorization: token ${{ secrets.mytoken }}" \
            -H "Content-Type: application/octet-stream" \
            "https://git.konsthol.eu/api/v1/repos/konsthol/WOL-Ly/releases/$(curl -s https://git.konsthol.eu/api/v1/repos/konsthol/WOL-Ly/releases/latest | jq -r '.id')/assets?name=wolServer-arm"
          else
            echo "ARM executable not found"
          fi

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

    steps:
      - name: Cleanup Buildx Containers
        run: |
          docker ps -q --filter "name=buildx" | xargs -r docker stop
          docker ps -aq --filter "name=buildx" | xargs -r docker rm
          docker images --filter "reference=moby/buildkit" -q | xargs -r docker rmi
          docker images --filter "reference=arm-builder" -q | xargs -r docker rmi
          docker images --filter "reference=multiarch/qemu-user-static" -q | xargs -r docker rmi
          docker images --filter "reference=tonistiigi/binfmt" -q | xargs -r docker rmi
          docker volume ls --filter "name=buildx" -q | xargs -r docker volume rm
          #docker images --filter "reference=gitea/runner-images" -q | xargs -r docker rmi
          #docker volume ls --filter "name=act" -q | xargs -r docker volume rm
