name: Build Offline FluxCD Installer

on:
  push:
    paths:
      - '.github/workflows/offline-package-fluxcd-installer.yaml'
  workflow_dispatch:
    inputs:
      tag:
        description: "Release tag to use/sync (e.g., v2.2.0). Leave empty to use offline-fluxcd-<run_number>"
        required: false
        type: string
      chart_version:
        description: "Override Helm chart version for fluxcd-community/flux2. Leave empty to auto-resolve"
        required: false
        type: string

permissions:
  contents: write

concurrency:
  group: build-offline-fluxcd
  cancel-in-progress: false

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

      - name: Install deps (curl, jq, helm)
        run: |
          set -euo pipefail
          sudo apt-get update -y
          sudo apt-get install -y curl jq
          curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
          helm version

      - name: Add helm repo
        run: |
          set -euo pipefail
          helm repo add fluxcd-community https://fluxcd-community.github.io/helm-charts --force-update
          helm repo update

      - name: Resolve chart version
        id: resolve
        env:
          OVERRIDE_CHART_VERSION: ${{ github.event.inputs.chart_version }}
        run: script/offline-fluxcd/resolve-chart-version.sh

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

      - name: Stage installer script
        env:
          CHART_VERSION: ${{ steps.resolve.outputs.chart_version }}
        run: script/offline-fluxcd/stage-installer.sh

      - name: Download nerdctl binary for ${{ matrix.arch }}
        run: |
          set -euo pipefail
          wget https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION}-linux-${{ matrix.arch }}.tar.gz \
            -O offline-installer/nerdctl.tar.gz

      - name: Pull & export required images
        env:
          CHART_VERSION: ${{ steps.resolve.outputs.chart_version }}
          MATRIX_ARCH: ${{ matrix.arch }}
        run: script/offline-fluxcd/pull-and-export-images.sh

      - name: Download Helm chart
        env:
          CHART_VERSION: ${{ steps.resolve.outputs.chart_version }}
        run: |
          set -euo pipefail
          helm pull fluxcd-community/flux2 --version "${CHART_VERSION}" --untar --untardir offline-installer/charts

      - name: Package offline installer
        run: |
          set -euo pipefail
          tar -czf offline-setup-fluxcd-${{ matrix.arch }}.tar.gz -C offline-installer .
          ls -lh offline-setup-fluxcd-${{ matrix.arch }}.tar.gz

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