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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT_DIR"

fail() {
  echo "[org-context-chart-package] $*" >&2
  exit 1
}

assert_contains() {
  local file="$1"
  local needle="$2"
  grep -Fq "$needle" "$file" || fail "$file must contain: $needle"
}

assert_contains orgfront/package.json "build:org-context-chart:min"
assert_contains orgfront/package.json "scripts/build-org-context-chart.mjs"
assert_contains orgfront/scripts/build-org-context-chart.mjs "ORG_CONTEXT_CHART_BUILD_ID"
assert_contains orgfront/vite.org-context-chart.config.ts "ORG_CONTEXT_CHART_MINIFY"
assert_contains orgfront/vite.org-context-chart.config.ts "ORG_CONTEXT_CHART_BUILD_ID"
assert_contains orgfront/vite.org-context-chart.config.ts "boc-"
assert_contains orgfront/vite.org-context-chart.config.ts ".min"
assert_contains orgfront/scripts/build-org-context-chart.mjs 'return `${year}${month}${random}`;'
assert_contains orgfront/vite.org-context-chart.config.ts 'return `${year}${month}${random}`;'

if grep -Fq '${year}${month}${day}' orgfront/scripts/build-org-context-chart.mjs orgfront/vite.org-context-chart.config.ts; then
  fail "OrgContext chart build id must use YYMM plus 4 random digits, without day or separators"
fi

echo "OK: OrgContext chart package emits timestamped short bundle names"
