name: Build Offline Argo CD Installer

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

permissions:
  contents: write

concurrency:
  group: build-offline-argocd
  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 argo https://argoproj.github.io/argo-helm --force-update
          helm repo update

      - name: Resolve chart version
        id: resolve
        env:
          OVERRIDE_CHART_VERSION: ${{ github.event.inputs.chart_version }}
        run: bash scripts/offline-argocd/resolve_chart_version.sh

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

      - name: Stage installer script
        env:
          CHART_VERSION: ${{ steps.resolve.outputs.chart_version }}
        run: bash scripts/offline-argocd/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 argocd-offline-package/nerdctl.tar.gz

      - name: Pull & export required images
        env:
          CHART_VERSION: ${{ steps.resolve.outputs.chart_version }}
          MATRIX_ARCH: ${{ matrix.arch }}
        run: bash scripts/offline-argocd/pull_and_export_images.sh

      - name: Download Helm chart
        env:
          CHART_VERSION: ${{ steps.resolve.outputs.chart_version }}
        run: |
          set -euo pipefail
          helm pull argo/argo-cd --version "${CHART_VERSION}" --untar --untardir argocd-offline-package/charts

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

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: offline-package-argocd-${{ matrix.arch }}
          path: offline-package-argocd-${{ 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-argocd-${{ matrix.arch }}
          path: offline-test

      - name: Verify offline package integrity
        run: |
          set -euo pipefail
          cd offline-test
          tar -tzf offline-package-argocd-${{ 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-argocd-{0}', github.run_number) }}
      RSYNC_SSH_KEY: ${{ secrets.RSYNC_SSH_KEY }}
      RSYNC_SSH_USER: ${{ secrets.RSYNC_SSH_USER }}
      VPS_HOST: ${{ secrets.VPS_HOST }}
      REMOTE_ROOT: /data/update-server/argocd
    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-argocd-amd64
          path: release-artifacts/amd64

      - name: Download arm64 artifact
        uses: actions/download-artifact@v4
        with:
          name: offline-package-argocd-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-argocd-amd64.tar.gz
            release-artifacts/arm64/offline-package-argocd-arm64.tar.gz
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Ensure deps (rsync, ssh)
        run: |
          set -euo pipefail
          sudo apt-get update -y
          sudo apt-get install -y rsync openssh-client

      - name: Init SSH
        run: |
          set -euo pipefail
          mkdir -p ~/.ssh
          echo "$RSYNC_SSH_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H "$VPS_HOST" >> ~/.ssh/known_hosts

      - name: Rsync release assets to remote
        run: bash scripts/offline-argocd/rsync_release_assets.sh

  retention:
    name: Remote retention (keep latest 3)
    needs: publish-release
    runs-on: ubuntu-latest
    env:
      RSYNC_SSH_KEY: ${{ secrets.RSYNC_SSH_KEY }}
      RSYNC_SSH_USER: ${{ secrets.RSYNC_SSH_USER }}
      VPS_HOST: ${{ secrets.VPS_HOST }}
      REMOTE_ROOT: /data/update-server/argocd
    steps:
      - name: Init SSH
        run: |
          set -euo pipefail
          mkdir -p ~/.ssh
          echo "$RSYNC_SSH_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H "$VPS_HOST" >> ~/.ssh/known_hosts

      - name: Prune old versions on remote (keep 3)
        run: bash scripts/offline-argocd/prune_remote_versions.sh
