name: Release & Multi-Platform Distribution

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write
  packages: write

jobs:
  # ----------------------------------------------------------------------------
  # 1. Linux Native Packages (.deb, .rpm, .tar.gz) via CMake + CTest + CPack
  # ----------------------------------------------------------------------------
  build-linux-packages:
    name: Build & Test Linux Packages (DEB, RPM, TGZ)
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v7

      - name: Install Build & Packaging Tools
        run: |
          sudo apt-get update -qq
          sudo apt-get install -y -qq build-essential cmake rpm

      - name: Configure CMake
        run: |
          cmake -B build -S . \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_C_EXTENSIONS=OFF \
            -DCMAKE_INSTALL_PREFIX=/usr \
            -DBUILD_TESTING=ON

      - name: Build Library, CLI & Tests
        run: cmake --build build -j$(nproc)

      - name: Run CTest Suite
        run: ctest --test-dir build --output-on-failure

      - name: Generate Packages
        run: |
          mkdir -p dist
          cpack --config build/CPackConfig.cmake -B dist
          rm -rf dist/_CPack_Packages

      - name: Upload Package Artifacts
        uses: actions/upload-artifact@v7
        with:
          name: linux-native-packages
          path: dist/*
          if-no-files-found: error

  # ----------------------------------------------------------------------------
  # 2. Alpine Linux musl Package (.tar.gz) via CMake + CTest + CPack
  # ----------------------------------------------------------------------------
  build-alpine-musl-package:
    name: Build & Test Alpine Linux musl Package
    runs-on: ubuntu-22.04
    container:
      image: alpine:3.20
    steps:
      - name: Install Build & Packaging Tools
        run: apk add --no-cache git build-base cmake linux-headers ffmpeg-dev

      - name: Checkout Source Code
        uses: actions/checkout@v7

      - name: Configure CMake
        run: |
          cmake -B build -S . \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_C_EXTENSIONS=OFF \
            -DCMAKE_INSTALL_PREFIX=/usr \
            -DBUILD_TESTING=ON

      - name: Build Library, CLI & Tests
        run: cmake --build build -j$(nproc)

      - name: Run CTest Suite
        run: ctest --test-dir build --output-on-failure

      - name: Generate musl Tarball Package
        run: |
          mkdir -p dist
          VERSION=$(grep 'PROJECT_VERSION:STATIC=' build/CMakeCache.txt | cut -d= -f2)
          cpack --config build/CPackConfig.cmake -G "TGZ" -D CPACK_PACKAGE_FILE_NAME="openavx-${VERSION}-linux-x86_64-musl" -B dist
          rm -rf dist/_CPack_Packages

      - name: Upload musl Package Artifacts
        uses: actions/upload-artifact@v7
        with:
          name: alpine-musl-package
          path: dist/*
          if-no-files-found: error

  # ----------------------------------------------------------------------------
  # 3. macOS Universal 2 Package (arm64 + x86_64)
  # ----------------------------------------------------------------------------
  build-macos:
    name: Build & Test macOS Universal 2 Package
    runs-on: macos-15
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v7

      - name: Configure Universal 2 Build
        run: |
          cmake -B build-macos -S . \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_C_EXTENSIONS=OFF \
            -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
            -DCMAKE_INSTALL_PREFIX=/usr/local \
            -DBUILD_TESTING=ON

      - name: Compile Universal Binaries & Tests
        run: cmake --build build-macos -j$(sysctl -n hw.ncpu)

      - name: Run CTest Suite
        run: ctest --test-dir build-macos --output-on-failure

      - name: Package macOS Tarball
        run: |
          mkdir -p dist
          cpack --config build-macos/CPackConfig.cmake -G "TGZ" -B dist
          rm -rf dist/_CPack_Packages

      - name: Upload macOS Artifacts
        uses: actions/upload-artifact@v7
        with:
          name: macos-packages
          path: dist/*
          if-no-files-found: error

  # ----------------------------------------------------------------------------
  # 4. Windows Native Package (x64 ZIP)
  # ----------------------------------------------------------------------------
  build-windows:
    name: Build & Test Windows Package (x64)
    runs-on: windows-2025
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v7

      - name: Configure CMake
        run: cmake -B build-win -S . -A x64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_EXTENSIONS=OFF -DBUILD_TESTING=ON

      - name: Build Solution & Tests
        run: cmake --build build-win --config Release -j4

      - name: Run CTest Suite
        run: ctest --test-dir build-win -C Release --output-on-failure

      - name: Package Windows ZIP
        run: |
          mkdir dist
          cpack --config build-win/CPackConfig.cmake -C Release -G "ZIP" -B dist
          if (Test-Path dist/_CPack_Packages) { Remove-Item -Recurse -Force dist/_CPack_Packages }

      - name: Upload Windows Artifacts
        uses: actions/upload-artifact@v7
        with:
          name: windows-packages
          path: dist/*
          if-no-files-found: error

  # ----------------------------------------------------------------------------
  # 5. Multi-Arch Docker Container Build (Native x86_64 & ARM64) & Push to GHCR
  # ----------------------------------------------------------------------------
  build-docker:
    name: Build Docker Container (${{ matrix.arch }})
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: linux/amd64
            runner: ubuntu-22.04
            arch: amd64
          - platform: linux/arm64
            runner: ubuntu-22.04-arm
            arch: arm64
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v7

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

      - name: Log in to GitHub Container Registry
        if: startsWith(github.ref, 'refs/tags/v')
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract Docker Metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Build and Test Alpine Container (PR / Dispatch)
        if: "!startsWith(github.ref, 'refs/tags/v')"
        uses: docker/build-push-action@v7
        with:
          context: .
          file: distribution/docker/Dockerfile
          platforms: ${{ matrix.platform }}
          push: false
          labels: ${{ steps.meta.outputs.labels }}

      - name: Build and Push Digest to GHCR (Release Tag)
        id: build
        if: startsWith(github.ref, 'refs/tags/v')
        uses: docker/build-push-action@v7
        with:
          context: .
          file: distribution/docker/Dockerfile
          platforms: ${{ matrix.platform }}
          labels: ${{ steps.meta.outputs.labels }}
          outputs: type=image,name=ghcr.io/${{ github.repository }},push-by-digest=true,name-canonical=true,push=true

      - name: Export Image Digest
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          mkdir -p /tmp/digests
          digest="${{ steps.build.outputs.digest }}"
          echo -n "${digest}" > "/tmp/digests/${digest#sha256:}"

      - name: Upload Digest Artifact
        if: startsWith(github.ref, 'refs/tags/v')
        uses: actions/upload-artifact@v7
        with:
          name: docker-digests-${{ matrix.arch }}
          path: /tmp/digests/*
          if-no-files-found: error

  merge-docker-manifest:
    name: Merge Multi-Arch Docker Manifest
    needs: build-docker
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-22.04
    steps:
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract Docker Metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Download Image Digests
        uses: actions/download-artifact@v7
        with:
          pattern: docker-digests-*
          path: /tmp/digests
          merge-multiple: true

      - name: Create Multi-Arch Manifest List
        working-directory: /tmp/digests
        run: |
          IMAGE_NAME=$(echo "ghcr.io/${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
          TAG_ARGS=""
          while IFS= read -r tag; do
            [ -n "$tag" ] && TAG_ARGS="${TAG_ARGS} -t ${tag}"
          done <<< "${{ steps.meta.outputs.tags }}"
          DIGEST_ARGS=""
          for f in *; do
            DIGEST_ARGS="${DIGEST_ARGS} ${IMAGE_NAME}@sha256:${f}"
          done
          docker buildx imagetools create ${TAG_ARGS} ${DIGEST_ARGS}

  # ----------------------------------------------------------------------------
  # 6. Publish GitHub Release with Cross-Platform Checksums
  # ----------------------------------------------------------------------------
  publish-release:
    name: Publish Release & Universal Assets
    needs: [build-linux-packages, build-alpine-musl-package, build-macos, build-windows, merge-docker-manifest]
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v7

      - name: Download Linux Packages
        uses: actions/download-artifact@v7
        with:
          name: linux-native-packages
          path: release-assets/

      - name: Download Alpine musl Packages
        uses: actions/download-artifact@v7
        with:
          name: alpine-musl-package
          path: release-assets/

      - name: Download macOS Packages
        uses: actions/download-artifact@v7
        with:
          name: macos-packages
          path: release-assets/

      - name: Download Windows Packages
        uses: actions/download-artifact@v7
        with:
          name: windows-packages
          path: release-assets/

      - name: Generate SHA-256 Checksums
        working-directory: release-assets
        run: |
          sha256sum * > SHA256SUMS
          cat SHA256SUMS

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: release-assets/*
          generate_release_notes: true
          name: OpenAVX ${{ github.ref_name }}
