name: Build Offline AutoGen Studio Installer

on:
  push:
    paths:
      - 'gitops/scripts/autogen-studio/**'
      - '.github/workflows/offline-package-autogen-studio.yaml'
  workflow_dispatch:
    inputs:
      tag:
        description: "Release tag to use/sync (e.g., v0.1.0). Leave empty to use offline-autogen-studio-<run_number>"
        required: false
        type: string
      autogen_tag:
        description: "AutoGen Studio container tag (default: latest)"
        required: false
        type: string

permissions:
  contents: write

concurrency:
  group: build-offline-autogen-studio
  cancel-in-progress: false

jobs:
  build-offline-installer:
    strategy:
      matrix:
        arch: [amd64, arm64]
    runs-on: ubuntu-latest
    outputs:
      autogen_tag: ${{ steps.resolve.outputs.autogen_tag }}
    steps:
      - uses: actions/checkout@v4

      - name: Resolve image tag
        id: resolve
        env:
          INPUT_AUTOGEN_TAG: ${{ github.event.inputs.autogen_tag }}
        run: |
          set -euo pipefail
          AUTOGEN_TAG=${INPUT_AUTOGEN_TAG:-latest}
          echo "autogen_tag=${AUTOGEN_TAG}" >> "$GITHUB_OUTPUT"

      - name: Prepare directories
        run: |
          set -euo pipefail
          rm -rf offline-installer
          mkdir -p offline-installer/{images,scripts}

      - name: Stage compose file and scripts
        env:
          AUTOGEN_TAG: ${{ steps.resolve.outputs.autogen_tag }}
        run: |
          set -euo pipefail
          cp gitops/scripts/autogen-studio/docker-compose.yaml offline-installer/docker-compose.yaml
          sed -i "s/__AUTOGEN_TAG__/${AUTOGEN_TAG}/g" offline-installer/docker-compose.yaml
          cp gitops/scripts/autogen-studio/deploy-autogen-studio.sh offline-installer/scripts/
          chmod +x offline-installer/scripts/deploy-autogen-studio.sh
          cat <<'README' > offline-installer/README.md
# Offline AutoGen Studio Installer

This archive contains container images and helper assets for deploying AutoGen Studio with Docker Compose.

## Contents
- `docker-compose.yaml`: Reference deployment manifest configured for the packaged images.
- `images/`: Pre-pulled container images saved as tar archives.
- `scripts/deploy-autogen-studio.sh`: Helper script to load the images and manage the compose stack.

## Usage
1. Extract the archive on a host with Docker/nerdctl available.
2. (Optional) Run `IMAGE_LOAD_TOOL=nerdctl ./scripts/deploy-autogen-studio.sh load-images` to import the images with nerdctl.
3. Start the stack: `./scripts/deploy-autogen-studio.sh up`.
4. Access AutoGen Studio at http://localhost:9090.

Adjust the compose file as needed before running the helper script.
README

      - name: Pull & export container images
        env:
          AUTOGEN_TAG: ${{ steps.resolve.outputs.autogen_tag }}
        run: |
          set -euo pipefail
          image="autogenstudio/autogen-studio:${AUTOGEN_TAG}"
          docker pull "$image"
          safe=$(echo "$image" | tr '/:' '-_')
          docker save "$image" -o "offline-installer/images/${safe}.tar"

      - name: Package offline installer
        run: |
          set -euo pipefail
          tar -czf offline-package-autogen-studio-${{ matrix.arch }}.tar.gz -C offline-installer .

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: offline-package-autogen-studio-${{ matrix.arch }}
          path: offline-package-autogen-studio-${{ matrix.arch }}.tar.gz

  test-offline-installer:
    needs: build-offline-installer
    strategy:
      matrix:
        arch: [amd64]
    runs-on: ubuntu-latest
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          name: offline-package-autogen-studio-${{ matrix.arch }}
          path: offline-test

      - name: Verify archive integrity
        run: |
          set -euo pipefail
          cd offline-test
          tar -tzf offline-package-autogen-studio-${{ matrix.arch }}.tar.gz > /dev/null

  publish-release:
    needs: test-offline-installer
    runs-on: ubuntu-latest
    env:
      TAG_NAME: ${{ github.event.inputs.tag != '' && github.event.inputs.tag || format('offline-autogen-studio-{0}', github.run_number) }}
    steps:
      - uses: actions/checkout@v4

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        with:
          tag_name: ${{ env.TAG_NAME }}
          release_name: Build ${{ env.TAG_NAME }}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Download amd64 artifact
        uses: actions/download-artifact@v4
        with:
          name: offline-package-autogen-studio-amd64
          path: release-artifacts/amd64

      - name: Download arm64 artifact
        uses: actions/download-artifact@v4
        with:
          name: offline-package-autogen-studio-arm64
          path: release-artifacts/arm64

      - name: Upload offline installers to GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ env.TAG_NAME }}
          files: |
            release-artifacts/amd64/offline-package-autogen-studio-amd64.tar.gz
            release-artifacts/arm64/offline-package-autogen-studio-arm64.tar.gz
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
