name: Build Lite Docker Image

on:
  workflow_dispatch:
    inputs:
      _notes_:
        description: '⚠️ Create lite Docker images only after non-trivial version releases.'
        required: false
        type: boolean
        default: false

permissions:
  contents: read
  id-token: write
  packages: write

jobs:
  build-and-push-lite:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.x"

      - name: Get latest tag
        id: get_tag
        run: |
          LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
          if [ -z "$LATEST_TAG" ]; then
            LATEST_TAG="sha-$(git rev-parse --short HEAD)"
            echo "No tags found, using commit SHA: $LATEST_TAG"
          else
            echo "Latest tag found: $LATEST_TAG"
          fi
          PACKAGE_VERSION="${LATEST_TAG#v}"
          echo "tag=$LATEST_TAG" >> $GITHUB_OUTPUT
          echo "package_version=$PACKAGE_VERSION" >> $GITHUB_OUTPUT

      - name: Prepare lite tag
        id: lite_tag
        run: |
          LITE_TAG="${{ steps.get_tag.outputs.tag }}-lite"
          echo "Lite image tag: $LITE_TAG"
          echo "lite_tag=$LITE_TAG" >> $GITHUB_OUTPUT

      - name: Update version definitions
        run: |
          python scripts/release/set_version.py --core-version "${{ steps.get_tag.outputs.package_version }}"
          grep '__version__ = ' lightrag/_version.py

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

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

      - name: Install cosign
        uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6  # v4.1.2

      - name: Extract metadata for Docker
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=raw,value=${{ steps.lite_tag.outputs.lite_tag }}
            type=raw,value=lite

      - name: Build and push lite Docker image
        id: build-and-push
        uses: docker/build-push-action@v7
        with:
          context: .
          file: ./Dockerfile.lite
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=min

      - name: Sign lite Docker image
        if: steps.build-and-push.outputs.digest != ''
        env:
          DIGEST: ${{ steps.build-and-push.outputs.digest }}
          TAGS: ${{ steps.meta.outputs.tags }}
        run: |
          set -euo pipefail
          echo "Signing manifest digest: $DIGEST"
          while IFS= read -r tag; do
            if [ -z "$tag" ]; then
              continue
            fi
            echo "Signing ${tag}@${DIGEST}"
            cosign sign --yes "${tag}@${DIGEST}"
          done <<< "$TAGS"

      - name: Output image details
        run: |
          echo "Lite Docker image built and pushed successfully!"
          echo "Image tag: ghcr.io/${{ github.repository }}:${{ steps.lite_tag.outputs.lite_tag }}"
          echo "Signed manifest digest: ${{ steps.build-and-push.outputs.digest }}"
          echo "Base Git tag used: ${{ steps.get_tag.outputs.tag }}"
