name: Nightly QA (Inlined)

on:
  push:
    branches: [fix/nightly-qa-stabilization]
  schedule:
    - cron: '0 2 * * *' # Every night at 02:00
  workflow_dispatch:

jobs:
  qa:
    name: 🧪 Quality Assurance
    runs-on: docker
    container:
      image: catthehacker/ubuntu:act-latest
    env:
      NEXT_PUBLIC_BASE_URL: https://testing.mb-grid-solutions.com
      GATEKEEPER_PASSWORD: ${{ secrets.GATEKEEPER_PASSWORD }}
    steps:
      - name: 🛑 Cleanup Workspace
        run: |
          rm -rf .next .turbo node_modules || true

      - 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: |
          REGISTRY="${{ vars.REGISTRY_HOST || 'npm.infra.mintel.me' }}"
          echo "@mintel:registry=https://$REGISTRY" > .npmrc
          echo "//$REGISTRY/:_authToken=${{ secrets.NPM_TOKEN || secrets.GITEA_PAT || secrets.MINTEL_PRIVATE_TOKEN || secrets.REGISTRY_PASS }}" >> .npmrc

      - name: 🏗️ Install Dependencies
        run: pnpm install --frozen-lockfile

      - name: 🛠️ Patch @mintel/cli
        run: |
          CLI_PATH=$(find node_modules -name index.js | grep "@mintel/cli/dist/index.js" | head -n 1)
          if [ -n "$CLI_PATH" ] && [ -f "$CLI_PATH" ]; then
            echo "Patching CLI at $CLI_PATH"
            sed -i '2{/^#!\/usr\/bin\/env node/d;}' "$CLI_PATH"
          fi

      - name: 🔍 Static Analysis
        id: static
        run: |
          echo "Running Lint..."
          pnpm run lint
          echo "Running Depcheck..."
          pnpm dlx depcheck . --ignores="framer-motion,lucide-react,next-intl,vitest,@testing-library/jest-dom,@testing-library/react,@vitejs/plugin-react,autoprefixer,axios,cheerio,eslint-config-next,happy-dom,html-validate,husky,jsdom,lint-staged,pa11y-ci,pino-pretty,postcss,prettier,puppeteer,tailwindcss,tsx,typescript,@mintel/eslint-config,@mintel/husky-config,@mintel/tsconfig,@tailwindcss/postcss,@types/node,@types/nodemailer,@types/react,@types/react-dom,@payloadcms/ui,@commitlint/*"

      - name: ♿ Accessibility Audit
        id: a11y
        continue-on-error: true
        run: pnpm run check:wcag

      - name: 🔗 Link Checker
        id: links
        continue-on-error: true
        run: |
          curl -sLO https://github.com/lycheeverse/lychee/releases/download/v0.14.3/lychee-v0.14.3-x86_64-unknown-linux-gnu.tar.gz
          tar -xzf lychee-v0.14.3-x86_64-unknown-linux-gnu.tar.gz
          mv lychee /usr/local/bin/
          lychee --accept 200,429,999 --exclude-mail --exclude "https://www.linkedin.com/.*" --exclude "https://fonts.gstatic.com/.*" https://testing.mb-grid-solutions.com

      - name: 🎭 Performance & Lighthouse
        id: perf
        run: pnpm exec mintel pagespeed test -- --collect.settings.preset=desktop

      - name: 🔔 Notify Status
        if: always()
        run: |
          STATUS="success"
          if [[ "${{ steps.static.outcome }}" != "success" || "${{ steps.perf.outcome }}" != "success" ]]; then
            STATUS="failure"
          fi
          
          PAYLOAD=$(cat <<EOF
          {
            "status": "$STATUS",
            "project": "mb-grid-solutions.com",
            "workflow": "Nightly QA",
            "details": {
              "Static": "${{ steps.static.outcome }}",
              "A11y": "${{ steps.a11y.outcome }}",
              "Links": "${{ steps.links.outcome }}",
              "Perf": "${{ steps.perf.outcome }}"
            },
            "url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
          }
          EOF
          )
          
          curl -X POST -H "Content-Type: application/json" \
               -d "$PAYLOAD" \
               ${{ secrets.WEBHOOK_URL }} || echo "Notification failed"
