name: Build & Deploy

on:
  push:
    branches:
      - main
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      skip_checks:
        description: 'Skip tests? (true/false)'
        required: false
        default: 'false'

concurrency:
  group: ${{ github.workflow }}-${{ (github.ref_type == 'tag' && !contains(github.ref_name, '-')) && 'prod' || (github.ref_name == 'main' && 'testing' || github.ref_name) }}
  cancel-in-progress: true

jobs:
  # ──────────────────────────────────────────────────────────────────────────────
  # JOB 1: Prepare Environment
  # ──────────────────────────────────────────────────────────────────────────────
  prepare:
    name: 🔍 Prepare
    runs-on: docker
    outputs:
      target:           ${{ steps.determine.outputs.target }}
      image_tag:        ${{ steps.determine.outputs.image_tag }}
      env_file:         ${{ steps.determine.outputs.env_file }}
      traefik_host:     ${{ steps.determine.outputs.traefik_host }}
      traefik_rule:     ${{ steps.determine.outputs.traefik_rule }}
      next_public_url:  ${{ steps.determine.outputs.next_public_url }}
      project_name:     ${{ steps.determine.outputs.project_name }}
      short_sha:        ${{ steps.determine.outputs.short_sha }}
    container:
      image: catthehacker/ubuntu:act-latest
    steps:
      - name: 🧹 Maintenance (High Density Cleanup)
        shell: bash
        run: |
          echo "Purging old build layers and dangling images..."
          docker image prune -f
          docker builder prune -f --filter "until=6h"

      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: 🔍 Environment ermitteln
        id: determine
        shell: bash
        run: |
          REF="${{ github.ref_name }}"
          SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
          DOMAIN="mb-grid-solutions.com"
          PRJ="mb-grid-solutions"

          if [[ "${{ github.ref_type }}" == "branch" && "$REF" == "main" ]]; then
            TARGET="testing"
            IMAGE_TAG="main-${SHORT_SHA}"
            ENV_FILE=".env.testing"
            TRAEFIK_HOST="testing.${DOMAIN}"
          elif [[ "${{ github.ref_type }}" == "tag" ]]; then
            if [[ "$REF" =~ ^v[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then
              TARGET="production"
              IMAGE_TAG="$REF"
              ENV_FILE=".env.prod"
              TRAEFIK_HOST="${DOMAIN}, www.${DOMAIN}"
            else
              TARGET="staging"
              IMAGE_TAG="$REF"
              ENV_FILE=".env.staging"
              TRAEFIK_HOST="staging.${DOMAIN}"
            fi
          else
            TARGET="skip"
          fi

          if [[ "$TARGET" != "skip" ]]; then
            # Standardize Traefik Rule
            if [[ "$TRAEFIK_HOST" == *","* ]]; then
              TRAEFIK_RULE=$(echo "$TRAEFIK_HOST" | sed 's/,/ /g' | awk '{for(i=1;i<=NF;i++) printf "Host(\`%s\`)%s", $i, (i==NF?"":" || ")}')
              PRIMARY_HOST=$(echo "$TRAEFIK_HOST" | cut -d',' -f1 | sed 's/ //g')
            else
              TRAEFIK_RULE="Host(\`$TRAEFIK_HOST\`)"
              PRIMARY_HOST="$TRAEFIK_HOST"
            fi

            {
              echo "target=$TARGET"
              echo "image_tag=$IMAGE_TAG"
              echo "env_file=$ENV_FILE"
              echo "traefik_host=$PRIMARY_HOST"
              echo "traefik_rule=$TRAEFIK_RULE"
              echo "next_public_url=https://$PRIMARY_HOST"
              echo "project_name=$PRJ-$TARGET"
              echo "short_sha=$SHORT_SHA"
            } >> "$GITHUB_OUTPUT"

            # ⏳ Wait for Upstream Packages/Images if Tagged
            if [[ "${{ github.ref_type }}" == "tag" ]]; then
              echo "🔎 Checking for @mintel dependencies in package.json..."
              # Extract any @mintel/ version (they should be synced in monorepo)
              UPSTREAM_VERSION=$(grep -o '"@mintel/.*": "[^"]*"' package.json | grep -v "next-utils" | cut -d'"' -f4 | sed 's/\^//; s/\~//' | sort -V | tail -1)
              TAG_TO_WAIT="v$UPSTREAM_VERSION"
              
              if [[ -n "$UPSTREAM_VERSION" && "$UPSTREAM_VERSION" != "workspace:"* ]]; then
                # 1. Discovery (Works without token for public repositories)
                UPSTREAM_SHA=$(git ls-remote --tags https://git.infra.mintel.me/mmintel/at-mintel.git "$TAG_TO_WAIT" | grep "$TAG_TO_WAIT" | tail -n1 | awk '{print $1}')
                
                if [[ -z "$UPSTREAM_SHA" ]]; then
                  echo "❌ Error: Tag $TAG_TO_WAIT not found in mmintel/at-mintel."
                  exit 1
                fi
                echo "✅ Tag verified: Found upstream SHA $UPSTREAM_SHA for $TAG_TO_WAIT"

                # 2. Status Check (Requires PAT for cross-repo API access)
                POLL_TOKEN="${{ secrets.GITEA_PAT || secrets.MINTEL_PRIVATE_TOKEN }}"
                
                if [[ -n "$POLL_TOKEN" ]]; then
                  echo "⏳ POLL_TOKEN found. Checking upstream build status..."
                  curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
                    "https://git.infra.mintel.me/mmintel/at-mintel/raw/branch/main/packages/infra/scripts/wait-for-upstream.sh" > wait-for-upstream.sh
                  chmod +x wait-for-upstream.sh
                  GITEA_TOKEN="$POLL_TOKEN" ./wait-for-upstream.sh "mmintel/at-mintel" "$TAG_TO_WAIT"
                else
                  echo "ℹ️ No PAT secret found. Skipping build status wait (Actions API is restricted)."
                  echo "   If this build fails, ensure that mmintel/at-mintel $TAG_TO_WAIT has finished its Docker build."
                fi
              fi
            fi
          else
             echo "target=skip" >> "$GITHUB_OUTPUT"
          fi

  # ──────────────────────────────────────────────────────────────────────────────
  # JOB 2: QA (Lint, Build Test)
  # ──────────────────────────────────────────────────────────────────────────────
  qa:
    name: 🧪 QA
    needs: prepare
    if: needs.prepare.outputs.target != 'skip'
    runs-on: docker
    container:
      image: catthehacker/ubuntu:act-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
      - name: Setup pnpm
        uses: pnpm/action-setup@v3
        with:
          version: 10
      - name: 🔐 Registry Auth
        run: |
          echo "@mintel:registry=https://${{ vars.REGISTRY_HOST || 'npm.infra.mintel.me' }}" > .npmrc
          echo "//${{ vars.REGISTRY_HOST || 'npm.infra.mintel.me' }}/:_authToken=${{ secrets.REGISTRY_PASS }}" >> .npmrc
      - name: Install dependencies
        run: |
          pnpm store prune
          pnpm install --no-frozen-lockfile
      - name: 🧪 QA Checks
        if: github.event.inputs.skip_checks != 'true'
        run: |
          pnpm lint
          pnpm exec tsc --noEmit
          pnpm test run
      - name: 🏗️ Build Test
        if: github.event.inputs.skip_checks != 'true'
        run: pnpm build

  # ──────────────────────────────────────────────────────────────────────────────
  # JOB 3: Build & Push
  # ──────────────────────────────────────────────────────────────────────────────
  build:
    name: 🏗️ Build
    needs: prepare
    if: needs.prepare.outputs.target != 'skip'
    runs-on: docker
    container:
      image: catthehacker/ubuntu:act-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: 🐳 Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: 🔐 Registry Login
        run: echo "${{ secrets.REGISTRY_PASS }}" | docker login registry.infra.mintel.me -u "${{ secrets.REGISTRY_USER }}" --password-stdin
      - name: 🏗️ Build and Push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          platforms: linux/amd64
          build-args: |
            NEXT_PUBLIC_BASE_URL=${{ needs.prepare.outputs.next_public_url }}
            NEXT_PUBLIC_TARGET=${{ needs.prepare.outputs.target }}
            NPM_TOKEN=${{ secrets.REGISTRY_PASS }}
          tags: registry.infra.mintel.me/mintel/mb-grid-solutions:${{ needs.prepare.outputs.image_tag }}
          cache-from: type=registry,ref=registry.infra.mintel.me/mintel/mb-grid-solutions:buildcache
          cache-to: type=registry,ref=registry.infra.mintel.me/mintel/mb-grid-solutions:buildcache,mode=max
          secrets: |
            "NPM_TOKEN=${{ secrets.REGISTRY_PASS }}"

  # ──────────────────────────────────────────────────────────────────────────────
  # JOB 4: Deploy
  # ──────────────────────────────────────────────────────────────────────────────
  deploy:
    name: 🚀 Deploy
    needs: [prepare, build, qa]
    runs-on: docker
    container:
      image: catthehacker/ubuntu:act-latest
    env:
      TARGET:               ${{ needs.prepare.outputs.target }}
      IMAGE_TAG:            ${{ needs.prepare.outputs.image_tag }}
      PROJECT_NAME:         ${{ needs.prepare.outputs.project_name }}
      NEXT_PUBLIC_BASE_URL: ${{ needs.prepare.outputs.next_public_url }}
      
      # Secrets mapping (Database & CMS)
      PAYLOAD_SECRET:          ${{ (needs.prepare.outputs.target == 'testing' && secrets.TESTING_PAYLOAD_SECRET) || (needs.prepare.outputs.target == 'staging' && secrets.STAGING_PAYLOAD_SECRET) || secrets.PAYLOAD_SECRET || secrets.DIRECTUS_SECRET || vars.PAYLOAD_SECRET || 'you-need-to-set-a-payload-secret' }}
      DATABASE_URI:            ${{ (needs.prepare.outputs.target == 'testing' && secrets.TESTING_DATABASE_URI) || (needs.prepare.outputs.target == 'staging' && secrets.STAGING_DATABASE_URI) || secrets.DATABASE_URI || vars.DATABASE_URI }}
      POSTGRES_DB:             ${{ secrets.POSTGRES_DB || vars.POSTGRES_DB || 'payload' }}
      POSTGRES_USER:           ${{ secrets.POSTGRES_USER || vars.POSTGRES_USER || 'directus' }}
      POSTGRES_PASSWORD:       ${{ (needs.prepare.outputs.target == 'testing' && secrets.TESTING_POSTGRES_PASSWORD) || (needs.prepare.outputs.target == 'staging' && secrets.STAGING_POSTGRES_PASSWORD) || secrets.POSTGRES_PASSWORD || vars.POSTGRES_PASSWORD || 'directus' }}

      
      # Secrets mapping (Mail)
      MAIL_HOST:               ${{ secrets.SMTP_HOST || vars.SMTP_HOST }}
      MAIL_PORT:               ${{ secrets.SMTP_PORT || vars.SMTP_PORT || '587' }}
      MAIL_USERNAME:           ${{ secrets.SMTP_USER || vars.SMTP_USER }}
      MAIL_PASSWORD:           ${{ secrets.SMTP_PASS || vars.SMTP_PASS }}
      MAIL_FROM:               ${{ secrets.SMTP_FROM || vars.SMTP_FROM }}
      MAIL_RECIPIENTS:         ${{ secrets.CONTACT_RECIPIENT || vars.CONTACT_RECIPIENT }}

      # Authentication
      GATEKEEPER_PASSWORD:     ${{ secrets.GATEKEEPER_PASSWORD || vars.GATEKEEPER_PASSWORD }}
      AUTH_COOKIE_NAME:        ${{ secrets.AUTH_COOKIE_NAME || vars.AUTH_COOKIE_NAME || 'mintel_gatekeeper_session' }}
      COOKIE_DOMAIN:           ${{ secrets.COOKIE_DOMAIN || vars.COOKIE_DOMAIN || '.mb-grid-solutions.com' }}

      # Monitoring & Services
      SENTRY_DSN:              ${{ secrets.SENTRY_DSN || vars.SENTRY_DSN }}
      UMAMI_WEBSITE_ID:        ${{ secrets.UMAMI_WEBSITE_ID || secrets.NEXT_PUBLIC_UMAMI_WEBSITE_ID || vars.UMAMI_WEBSITE_ID || vars.NEXT_PUBLIC_UMAMI_WEBSITE_ID }}
      UMAMI_API_ENDPOINT:      ${{ secrets.UMAMI_API_ENDPOINT || secrets.NEXT_PUBLIC_UMAMI_SCRIPT_URL || vars.UMAMI_API_ENDPOINT || 'https://analytics.infra.mintel.me' }}
      PROJECT_COLOR:           ${{ secrets.PROJECT_COLOR || vars.PROJECT_COLOR || '#82ed20' }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: 📝 Generate Environment
        shell: bash
        env:
          TRAEFIK_HOST:     ${{ needs.prepare.outputs.traefik_host }}
          ENV_FILE:         ${{ needs.prepare.outputs.env_file }}
        run: |
          # Middleware & Auth Logic
          LOG_LEVEL=$( [[ "$TARGET" == "testing" || "$TARGET" == "development" ]] && echo "debug" || echo "info" )
          STD_MW="${PROJECT_NAME}-forward,compress"
          
          if [[ "$TARGET" == "production" ]]; then
            AUTH_MIDDLEWARE="$STD_MW"
            COMPOSE_PROFILES=""
          else
            # Order: Forward (Proto) -> Auth -> Compression
            AUTH_MIDDLEWARE="${PROJECT_NAME}-forward,${PROJECT_NAME}-auth,compress"
            COMPOSE_PROFILES="gatekeeper"
          fi

          # Gatekeeper Origin
          COOKIE_DOMAIN=.$(echo $NEXT_PUBLIC_BASE_URL | sed 's|https://||')
          GATEKEEPER_ORIGIN="$NEXT_PUBLIC_BASE_URL/gatekeeper"

          # Generate Environment File
          cat > .env.deploy << EOF
          # Generated by CI - $TARGET
          IMAGE_TAG=$IMAGE_TAG
          NEXT_PUBLIC_BASE_URL=$NEXT_PUBLIC_BASE_URL
          GATEKEEPER_ORIGIN=$GATEKEEPER_ORIGIN
          SENTRY_DSN=$SENTRY_DSN
          PROJECT_COLOR=$PROJECT_COLOR
          LOG_LEVEL=$LOG_LEVEL
          
          # Database & Payload
          DATABASE_URI=\${DATABASE_URI:-postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@mb-grid-db:5432/$POSTGRES_DB}
          PAYLOAD_SECRET=${PAYLOAD_SECRET:-you-need-to-set-a-payload-secret}
          POSTGRES_DB=$POSTGRES_DB
          POSTGRES_USER=$POSTGRES_USER
          POSTGRES_PASSWORD=$POSTGRES_PASSWORD
          
          # Mail
          MAIL_HOST=$MAIL_HOST
          MAIL_PORT=$MAIL_PORT
          MAIL_USERNAME=$MAIL_USERNAME
          MAIL_PASSWORD=$MAIL_PASSWORD
          MAIL_FROM=$MAIL_FROM
          MAIL_RECIPIENTS=$MAIL_RECIPIENTS

          # Authentication
          GATEKEEPER_PASSWORD=$GATEKEEPER_PASSWORD
          AUTH_COOKIE_NAME=$AUTH_COOKIE_NAME
          COOKIE_DOMAIN=$COOKIE_DOMAIN

          # Analytics
          UMAMI_WEBSITE_ID=$UMAMI_WEBSITE_ID
          NEXT_PUBLIC_UMAMI_WEBSITE_ID=$UMAMI_WEBSITE_ID
          UMAMI_API_ENDPOINT=$UMAMI_API_ENDPOINT

          TARGET=$TARGET
          SENTRY_ENVIRONMENT=$TARGET
          PROJECT_NAME=$PROJECT_NAME
          ENV_FILE=$ENV_FILE
          TRAEFIK_HOST=$TRAEFIK_HOST
          COMPOSE_PROFILES=$COMPOSE_PROFILES
          TRAEFIK_MIDDLEWARES=$AUTH_MIDDLEWARE
          EOF

      - name: 🚀 SSH Deploy
        shell: bash
        env:
          ENV_FILE:         ${{ needs.prepare.outputs.env_file }}
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.ALPHA_SSH_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan -H alpha.mintel.me >> ~/.ssh/known_hosts 2>/dev/null

          # Transfer and Restart
          SITE_DIR="/home/deploy/sites/mb-grid-solutions.com"
          ssh root@alpha.mintel.me "mkdir -p $SITE_DIR"
          
          scp .env.deploy root@alpha.mintel.me:$SITE_DIR/$ENV_FILE
          scp docker-compose.yaml root@alpha.mintel.me:$SITE_DIR/docker-compose.yaml
          
          ssh root@alpha.mintel.me "cd $SITE_DIR && echo '${{ secrets.REGISTRY_PASS }}' | docker login registry.infra.mintel.me -u '${{ secrets.REGISTRY_USER }}' --password-stdin"
          ssh root@alpha.mintel.me "cd $SITE_DIR && docker compose -p '${{ needs.prepare.outputs.project_name }}' --env-file '$ENV_FILE' pull"
          ssh root@alpha.mintel.me "cd $SITE_DIR && docker compose -p '${{ needs.prepare.outputs.project_name }}' --env-file '$ENV_FILE' up -d --remove-orphans"
          
          # Apply Payload Migrations using the target app container's programmatic endpoint
          ssh root@alpha.mintel.me "cd $SITE_DIR && echo '→ Waiting for DB and Running Payload Migrations...' && \
            for i in {1..10}; do \
              echo \"Attempt \$i...\"; \
              docker compose -p '${{ needs.prepare.outputs.project_name }}' --env-file '$ENV_FILE' exec -T mb-grid-app sh -c 'curl -s -f -X POST -H \"Authorization: Bearer \$PAYLOAD_SECRET\" http://localhost:3000/api/payload/migrate \
                || { echo \"HTTP error or DB not ready.\"; exit 1; }' && { echo '✅ Migrations successful!'; break; } \
              || { if [ \$i -eq 10 ]; then echo '❌ Migration failed after 10 attempts!'; exit 1; else echo '⏳ Retrying in 5s...'; sleep 5; fi; }; \
            done"
          
          ssh root@alpha.mintel.me "docker system prune -f --filter 'until=24h'"

      - name: 🧹 Post-Deploy Cleanup (Runner)
        if: always()
        run: docker builder prune -f --filter "until=1h"

  # ──────────────────────────────────────────────────────────────────────────────
  # JOB 5: Post-Deploy Verification (Smoke Tests + Quality Gates)
  # ──────────────────────────────────────────────────────────────────────────────
  post_deploy_checks:
    name: 🧪 Post-Deploy Verification
    needs: [prepare, deploy]
    if: needs.deploy.result == 'success' && needs.prepare.outputs.target != 'branch'
    runs-on: docker
    container:
      image: catthehacker/ubuntu:act-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Setup pnpm
        uses: pnpm/action-setup@v3
        with:
          version: 10
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
      - name: 🔐 Registry Auth
        run: |
          echo "@mintel:registry=https://${{ vars.REGISTRY_HOST || 'npm.infra.mintel.me' }}" > .npmrc
          echo "//${{ vars.REGISTRY_HOST || 'npm.infra.mintel.me' }}/:_authToken=${{ secrets.REGISTRY_PASS }}" >> .npmrc
      - name: Install dependencies
        id: deps
        run: |
          pnpm store prune
          pnpm install --no-frozen-lockfile
      - name: 📦 Cache APT Packages
        uses: actions/cache@v4
        with:
          path: /var/cache/apt/archives
          key: apt-cache-${{ runner.os }}-${{ runner.arch }}-chromium

      - name: 💾 Cache Chromium
        id: cache-chromium
        uses: actions/cache@v4
        with:
          path: /usr/bin/chromium
          key: ${{ runner.os }}-chromium-native-${{ hashFiles('package.json') }}
          
      - name: 🔍 Install Chromium (Native & ARM64)
        if: steps.cache-chromium.outputs.cache-hit != 'true'
        run: |
          rm -f /etc/apt/apt.conf.d/docker-clean
          apt-get update
          apt-get install -y gnupg wget ca-certificates
          OS_ID=$(. /etc/os-release && echo $ID)
          CODENAME=$(. /etc/os-release && echo $VERSION_CODENAME)
          if [ "$OS_ID" = "debian" ]; then
            apt-get install -y chromium
          else
            mkdir -p /etc/apt/keyrings
            KEY_ID="82BB6851C64F6880"
            wget -qO- "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x$KEY_ID" | gpg --dearmor > /etc/apt/keyrings/xtradeb.gpg
            echo "deb [signed-by=/etc/apt/keyrings/xtradeb.gpg] http://ppa.launchpad.net/xtradeb/apps/ubuntu $CODENAME main" > /etc/apt/sources.list.d/xtradeb-ppa.list
            printf "Package: *\nPin: release o=LP-PPA-xtradeb-apps\nPin-Priority: 1001\n" > /etc/apt/preferences.d/xtradeb
            apt-get update
            apt-get install -y --allow-downgrades chromium
          fi
          [ -f /usr/bin/chromium ] && ln -sf /usr/bin/chromium /usr/bin/google-chrome
          [ -f /usr/bin/chromium ] && ln -sf /usr/bin/chromium /usr/bin/chromium-browser

      # ── Critical Smoke Tests (MUST pass) ──────────────────────────────────
      - name: 🏥 CMS Deep Health Check
        env:
          DEPLOY_URL: ${{ needs.prepare.outputs.next_public_url }}
          GK_PASS: ${{ secrets.GATEKEEPER_PASSWORD || vars.GATEKEEPER_PASSWORD }}
        run: |
          echo "Waiting 10s for app to fully start..."
          sleep 10
          echo "Checking basic health..."
          curl -sf "$DEPLOY_URL/api/health" || { echo "❌ Basic health check failed"; exit 1; }
          echo "✅ Basic health OK"

      - name: 🌐 Core Smoke Tests (HTTP, API, Locale)
        if: always() && steps.deps.outcome == 'success'
        uses: https://git.infra.mintel.me/mmintel/at-mintel/.gitea/actions/core-smoke-tests@main
        with:
          TARGET_URL: ${{ needs.prepare.outputs.next_public_url }}
          GATEKEEPER_PASSWORD: ${{ secrets.GATEKEEPER_PASSWORD || vars.GATEKEEPER_PASSWORD }}
          UMAMI_API_ENDPOINT: ${{ secrets.UMAMI_API_ENDPOINT || secrets.NEXT_PUBLIC_UMAMI_SCRIPT_URL || vars.UMAMI_API_ENDPOINT || 'https://analytics.infra.mintel.me' }}
          SENTRY_DSN: ${{ secrets.SENTRY_DSN || vars.SENTRY_DSN }}

      - name: 📝 E2E Form Submission Test
        if: always() && steps.deps.outcome == 'success'
        env:
          NEXT_PUBLIC_BASE_URL: ${{ needs.prepare.outputs.next_public_url }}
          GATEKEEPER_PASSWORD: ${{ secrets.GATEKEEPER_PASSWORD || vars.GATEKEEPER_PASSWORD }}
          PUPPETEER_EXECUTABLE_PATH: /usr/bin/chromium
        run: pnpm test run

  # ──────────────────────────────────────────────────────────────────────────────
  # JOB 6: Notifications
  # ──────────────────────────────────────────────────────────────────────────────
  notifications:
    name: 🔔 Notify
    needs: [prepare, deploy, post_deploy_checks]
    if: always()
    runs-on: docker
    container:
      image: catthehacker/ubuntu:act-latest
    steps:
      - name: 🔔 Gotify
        shell: bash
        run: |
          DEPLOY="${{ needs.deploy.result }}"
          SMOKE="${{ needs.post_deploy_checks.result }}"
          PERF="${{ needs.post_deploy_checks.result }}"
          TARGET="${{ needs.prepare.outputs.target }}"
          VERSION="${{ needs.prepare.outputs.image_tag }}"
          URL="${{ needs.prepare.outputs.next_public_url }}"

          # Gotify priority scale:
          #   1-3  = low (silent/info)
          #   4-5  = normal
          #   6-7  = high (warning)
          #   8-10 = critical (alarm)
          if [[ "$DEPLOY" != "success" ]]; then
            PRIORITY=10
            EMOJI="🚨"
            STATUS_LINE="DEPLOY FAILED"
          elif [[ "$SMOKE" != "success" ]]; then
            PRIORITY=8
            EMOJI="⚠️"
            STATUS_LINE="Smoke tests failed"
          elif [[ "$PERF" != "success" ]]; then
            PRIORITY=5
            EMOJI="📉"
            STATUS_LINE="Performance degraded"
          else
            PRIORITY=2
            EMOJI="✅"
            STATUS_LINE="All checks passed"
          fi

          TITLE="$EMOJI mb-grid-solutions.com $VERSION → $TARGET"
          MESSAGE="$STATUS_LINE
          Deploy: $DEPLOY | Smoke: $SMOKE | Perf: $PERF
          $URL"

          curl -s -k -X POST "${{ secrets.GOTIFY_URL }}/message?token=${{ secrets.GOTIFY_TOKEN }}" \
            -F "title=$TITLE" \
            -F "message=$MESSAGE" \
            -F "priority=$PRIORITY" || true
