#!/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"
mkdir -p "$WORK_DIR/badge-artifacts/backend/reports"
mkdir -p "$WORK_DIR/badge-artifacts/userfront/reports"
mkdir -p "$WORK_DIR/badge-artifacts/adminfront/reports"
mkdir -p "$WORK_DIR/badge-artifacts/orgfront/reports"

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": {
    "userfront": {
      "label": "userfront",
      "message": "unknown",
      "color": "#6e7781"
    },
    "adminfront": {
      "label": "adminfront",
      "message": "10.00%",
      "color": "#cf222e"
    },
    "devfront": {
      "label": "devfront",
      "message": "20.00%",
      "color": "#cf222e"
    },
    "orgfront": {
      "label": "orgfront",
      "message": "30.00%",
      "color": "#cf222e"
    }
  }
}
JSON

cat > "$WORK_DIR/badge-artifacts/backend/reports/backend-coverage-summary.json" <<'JSON'
{
  "package": "backend",
  "statements": 90.0
}
JSON

cat > "$WORK_DIR/badge-artifacts/userfront/reports/package-coverage-summary.json" <<'JSON'
{
  "packages": [
    {
      "package": "userfront",
      "statements": 85.4
    }
  ]
}
JSON

cat > "$WORK_DIR/badge-artifacts/adminfront/reports/vitest-coverage-summary.json" <<'JSON'
{
  "packages": [
    {
      "package": "adminfront",
      "statements": 82.345
    }
  ]
}
JSON

cat > "$WORK_DIR/badge-artifacts/orgfront/reports/vitest-coverage-summary.json" <<'JSON'
{
  "packages": [
    {
      "package": "orgfront",
      "statements": 36.1
    }
  ]
}
JSON

run_badge_update() {
  (
    cd "$WORK_DIR"
    LINT_RESULT=success \
      BIOME_RESULT=success \
      BACKEND_RESULT=success \
      BACKEND_COVERAGE_RESULT=success \
      USERFRONT_RESULT=success \
      USERFRONT_E2E_RESULT=success \
      USERFRONT_E2E_CHROMIUM_DESKTOP_RESULT=success \
      USERFRONT_E2E_CHROMIUM_MOBILE_RESULT=failure \
      USERFRONT_E2E_FIREFOX_DESKTOP_RESULT=success \
      USERFRONT_E2E_FIREFOX_MOBILE_RESULT=skipped \
      USERFRONT_E2E_WEBKIT_DESKTOP_RESULT=failure \
      USERFRONT_E2E_WEBKIT_MOBILE_RESULT=success \
      ADMINFRONT_RESULT=success \
      DEVFRONT_RESULT=success \
      ORGFRONT_RESULT=success \
      USERFRONT_COVERAGE_RESULT=success \
      ADMINFRONT_COVERAGE_RESULT=success \
      DEVFRONT_COVERAGE_RESULT=failure \
      ORGFRONT_COVERAGE_RESULT=success \
      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"
  )
}

run_badge_update

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["backend-tests"].label, "backend", "backend badge label must be compact");
assertEqual(badges["backend-tests"].message, "pass | 90.00%", "backend badge must combine test result and coverage");
assertEqual(badges.userfront.message, "pass | 85.40%", "userfront badge must combine fast E2E result and coverage");
assertEqual(badges.adminfront.message, "pass | 82.34%", "adminfront badge must combine E2E result and coverage");
assertEqual(badges.devfront.message, "pass | fail", "devfront badge must fail coverage independently");
assertEqual(badges.orgfront.message, "pass | 36.10%", "orgfront badge must combine E2E result and coverage");
assertEqual(badges["userfront-chrome"].label, "chrome", "chromium full badge label must name the browser");
assertEqual(badges["userfront-chrome"].message, "pass | fail", "chromium full badge must show desktop and mobile results");
assertEqual(badges["userfront-firefox"].label, "firefox", "firefox full badge label must name the browser");
assertEqual(badges["userfront-firefox"].message, "pass | skip", "firefox full badge must show desktop and mobile results");
assertEqual(badges["userfront-safari"].label, "safari", "webkit full badge label must be shown as safari");
assertEqual(badges["userfront-safari"].message, "fail | pass", "webkit full badge must show desktop and mobile results");
NODE

cp "$WORK_DIR/docs/badges/badges.json" "$WORK_DIR/first-badges.json"
run_badge_update
cmp "$WORK_DIR/first-badges.json" "$WORK_DIR/docs/badges/badges.json"

echo "OK: package coverage badges update independently and avoid rerun churn"
