#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
WORK_DIR="$(mktemp -d)"
trap 'rm -rf "$WORK_DIR"' EXIT

mkdir -p "$WORK_DIR/docs/badges"

cat > "$WORK_DIR/docs/badges/badges.json" <<'JSON'
{
  "schemaVersion": 1,
  "generatedBy": "scripts/update_code_check_badges.mjs",
  "updatedAt": "2026-01-01T00:00:00.000Z",
  "source": {
    "branch": "dev",
    "sha": "abc123456789",
    "shortSha": "abc123456789",
    "runId": "1",
    "runNumber": "1"
  },
  "badges": {
    "code-check": {
      "label": "code check",
      "message": "passing",
      "color": "#2ea043"
    },
    "biome": {
      "label": "biome",
      "message": "passing",
      "color": "#2ea043"
    },
    "backend-tests": {
      "label": "backend",
      "message": "pass | 93.20%",
      "color": "#2ea043"
    },
    "userfront": {
      "label": "userfront",
      "message": "pass | 56.52%",
      "color": "#bf8700"
    },
    "adminfront": {
      "label": "adminfront",
      "message": "pass | 60.01%",
      "color": "#bf8700"
    },
    "devfront": {
      "label": "devfront",
      "message": "pass | 58.02%",
      "color": "#bf8700"
    },
    "orgfront": {
      "label": "orgfront",
      "message": "pass | 61.18%",
      "color": "#bf8700"
    },
    "userfront-chrome": {
      "label": "chrome",
      "message": "unknown",
      "color": "#6e7781"
    },
    "userfront-firefox": {
      "label": "firefox",
      "message": "unknown",
      "color": "#6e7781"
    },
    "userfront-safari": {
      "label": "safari",
      "message": "unknown",
      "color": "#6e7781"
    }
  }
}
JSON

(
  cd "$WORK_DIR"
  USERFRONT_E2E_RESULT=failure \
    USERFRONT_E2E_FULL=true \
    USERFRONT_E2E_CHROMIUM_DESKTOP_RESULT=success \
    USERFRONT_E2E_CHROMIUM_MOBILE_RESULT=success \
    USERFRONT_E2E_FIREFOX_DESKTOP_RESULT=success \
    USERFRONT_E2E_FIREFOX_MOBILE_RESULT=skipped \
    USERFRONT_E2E_WEBKIT_DESKTOP_RESULT=failure \
    USERFRONT_E2E_WEBKIT_MOBILE_RESULT=success \
    BADGE_UPDATE_CODE_CHECK=false \
    BADGE_SOURCE_BRANCH=dev \
    BADGE_SOURCE_SHA=abc123456789 \
    GITHUB_RUN_ID=2 \
    GITHUB_RUN_NUMBER=2 \
    node "$ROOT_DIR/scripts/update_code_check_badges.mjs"
)

node - "$WORK_DIR/docs/badges/badges.json" <<'NODE'
const fs = require("node:fs");

const manifest = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
const badges = manifest.badges;

function assertEqual(actual, expected, message) {
  if (actual !== expected) {
    throw new Error(`${message}: expected ${expected}, got ${actual}`);
  }
}

assertEqual(badges["code-check"].message, "passing", "full nightly must preserve code-check badge");
assertEqual(badges.biome.message, "passing", "full nightly must preserve biome badge");
assertEqual(badges["backend-tests"].message, "pass | 93.20%", "full nightly must preserve backend badge");
assertEqual(badges.userfront.message, "pass | 56.52%", "full nightly must preserve userfront package badge");
assertEqual(badges.adminfront.message, "pass | 60.01%", "full nightly must preserve adminfront badge");
assertEqual(badges.devfront.message, "pass | 58.02%", "full nightly must preserve devfront badge");
assertEqual(badges.orgfront.message, "pass | 61.18%", "full nightly must preserve orgfront badge");
assertEqual(badges["userfront-chrome"].message, "pass | pass", "full nightly must update chrome badge");
assertEqual(badges["userfront-firefox"].message, "pass | skip", "full nightly must update firefox badge");
assertEqual(badges["userfront-safari"].message, "fail | pass", "full nightly must update safari badge");
NODE

echo "OK: full nightly preserves existing non-browser badges"
