name: Publish Docker image to Docker Hub

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

jobs:
  prepare:
    name: Prepare metadata
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      version: ${{ steps.notes.outputs.version }}
      desc: ${{ steps.notes.outputs.desc }}
      labels: ${{ steps.meta.outputs.labels }}
    steps:
      - name: Checkout
        uses: actions/checkout@v5.0.0

      - name: Prepare release notes from template
        id: notes
        shell: bash
        run: |
          VERSION_REF="${GITHUB_REF##*/}"  # e.g. v1.2.3
          TEMPLATE="RELEASE_NOTES_TEMPLATE.md"
          if [ -f "$TEMPLATE" ]; then
            sed "s/\${VERSION}/${VERSION_REF}/g" "$TEMPLATE" > RELEASE_NOTES.md
          else
            echo "# MTG Python Deckbuilder ${VERSION_REF}" > RELEASE_NOTES.md
            echo >> RELEASE_NOTES.md
            echo "Automated release." >> RELEASE_NOTES.md
          fi
          DESC=$(awk 'BEGIN{ORS="\\n"} {print}' RELEASE_NOTES.md)
          echo "desc=$DESC" >> $GITHUB_OUTPUT
          echo "version=$VERSION_REF" >> $GITHUB_OUTPUT

      - name: Extract Docker metadata (latest only)
        id: meta
        uses: docker/metadata-action@v5.8.0
        with:
          images: |
            mwisnowski/mtg-python-deckbuilder
          tags: |
            type=raw,value=latest
          labels: |
            org.opencontainers.image.title=MTG Python Deckbuilder
            org.opencontainers.image.version=${{ github.ref_name }}
            org.opencontainers.image.description=${{ steps.notes.outputs.desc }}
            org.opencontainers.image.revision=${{ github.sha }}

  build_amd64:
    name: Build (amd64)
    runs-on: ubuntu-latest
    needs: prepare
    permissions:
      contents: read
    outputs:
      digest: ${{ steps.build.outputs.digest }}
    steps:
      - name: Checkout
        uses: actions/checkout@v5.0.0

      - name: Download similarity cache from branch
        run: |
          # Download cache files from similarity-cache-data branch
          mkdir -p card_files
          wget -q https://raw.githubusercontent.com/${{ github.repository }}/similarity-cache-data/card_files/similarity_cache.parquet -O card_files/similarity_cache.parquet || echo "Cache not found, will build without it"
          wget -q https://raw.githubusercontent.com/${{ github.repository }}/similarity-cache-data/card_files/similarity_cache_metadata.json -O card_files/similarity_cache_metadata.json || echo "Metadata not found"
          
          if [ -f card_files/similarity_cache.parquet ]; then
            echo "✓ Downloaded similarity cache"
            ls -lh card_files/similarity_cache.parquet
          fi

      - name: Compute amd64 tag
        id: arch_tag
        shell: bash
        run: |
          echo "tag=mwisnowski/mtg-python-deckbuilder:${{ needs.prepare.outputs.version }}-amd64" >> $GITHUB_OUTPUT

      - name: Docker Hub login
        uses: docker/login-action@v3.5.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

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

      - name: Smoke test Web UI (local build)
        shell: bash
        env:
          APP_VERSION: ${{ needs.prepare.outputs.version }}
        run: |
          docker buildx build --platform linux/amd64 --load -t mtg-deckbuilder:test --build-arg APP_VERSION=$APP_VERSION .
          docker rm -f mtg-smoke 2>/dev/null || true
          docker run -d --name mtg-smoke -p 8080:8080 mtg-deckbuilder:test
          echo "Waiting for Web UI (amd64)..."
          for i in {1..30}; do
            if curl -fsS http://localhost:8080/ >/dev/null; then echo "Up"; break; fi
            sleep 2
          done
          if ! curl -fsS http://localhost:8080/ >/dev/null; then
            echo "Web UI did not start in time. Logs:" && docker logs mtg-smoke || true
            exit 1
          fi
          docker rm -f mtg-smoke >/dev/null 2>&1 || true

      - name: Build & push arch image (amd64)
        id: build
        uses: docker/build-push-action@v6.18.0
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: linux/amd64
          tags: ${{ steps.arch_tag.outputs.tag }}
          labels: ${{ needs.prepare.outputs.labels }}
          build-args: |
            APP_VERSION=${{ needs.prepare.outputs.version }}

  build_arm64:
    name: Build (arm64)
    runs-on: ubuntu-24.04-arm
    needs: prepare
    permissions:
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v5.0.0

      - name: Download similarity cache from branch
        run: |
          # Download cache files from similarity-cache-data branch
          mkdir -p card_files
          wget -q https://raw.githubusercontent.com/${{ github.repository }}/similarity-cache-data/card_files/similarity_cache.parquet -O card_files/similarity_cache.parquet || echo "Cache not found, will build without it"
          wget -q https://raw.githubusercontent.com/${{ github.repository }}/similarity-cache-data/card_files/similarity_cache_metadata.json -O card_files/similarity_cache_metadata.json || echo "Metadata not found"
          
          if [ -f card_files/similarity_cache.parquet ]; then
            echo "✓ Downloaded similarity cache"
            ls -lh card_files/similarity_cache.parquet
          fi

      - name: Compute arm64 tag
        id: arch_tag
        shell: bash
        run: |
          echo "tag=mwisnowski/mtg-python-deckbuilder:${{ needs.prepare.outputs.version }}-arm64" >> $GITHUB_OUTPUT

      - name: Docker Hub login
        uses: docker/login-action@v3.5.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up QEMU (for emulation)
        uses: docker/setup-qemu-action@v3.6.0

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

      - name: Build & push arch image (arm64)
        uses: docker/build-push-action@v6.18.0
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: linux/arm64
          tags: ${{ steps.arch_tag.outputs.tag }}
          labels: ${{ needs.prepare.outputs.labels }}
          build-args: |
            APP_VERSION=${{ needs.prepare.outputs.version }}

  manifest:
    name: Create latest multi-arch manifest
    runs-on: ubuntu-latest
    needs: [prepare, build_amd64, build_arm64]
    steps:
      - name: Docker Hub login
        uses: docker/login-action@v3.5.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Create & push latest multi-arch manifest
        shell: bash
        run: |
          set -euo pipefail
          VERSION='${{ needs.prepare.outputs.version }}'
          AMD_TAG="mwisnowski/mtg-python-deckbuilder:${VERSION}-amd64"
          ARM_TAG="mwisnowski/mtg-python-deckbuilder:${VERSION}-arm64"
          echo "Creating manifest: latest -> ${AMD_TAG} + ${ARM_TAG}"
          SOURCES="$AMD_TAG $ARM_TAG"
          docker buildx imagetools create -t mwisnowski/mtg-python-deckbuilder:latest $SOURCES
          echo "Inspecting latest"
          docker buildx imagetools inspect mwisnowski/mtg-python-deckbuilder:latest

