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

ROOT="${AGENTTEAM_REPO_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)}"
PRIMITIVES="$ROOT/web/src/agentteam/workspace/sidebar/sidebarPrimitives.tsx"
CONVERSATION_COMPONENTS="$ROOT/web/src/agentteam/workspace/conversation/components"

python3 - "$PRIMITIVES" "$CONVERSATION_COMPONENTS" <<'PY'
from pathlib import Path
import re
import sys

primitives_path = Path(sys.argv[1])
conversation_components = Path(sys.argv[2])
primitives = primitives_path.read_text()
failures: list[str] = []

props_match = re.search(
    r"type SidebarItemButtonProps = \{(?P<body>.*?)\n\}", primitives, re.S
)
if props_match is None:
    failures.append("SidebarItemButtonProps is missing")
else:
    props = props_match.group("body")
    required = {
        "title": r"\btitle: string\b",
        "title metadata": r"\btitleMeta\?: string\b",
        "timestamp": r"\btimestamp\?: string\b",
        "secondary summary": r"\bsecondary\?: string\b",
        "secondary semantic state": r"\bsecondaryState\?: SidebarItemSecondaryState\b",
        "secondary timestamp": r"\bsecondaryTimestamp\?: string\b",
    }
    for label, pattern in required.items():
        if re.search(pattern, props) is None:
            failures.append(f"SidebarItemButton must own a string-only {label} contract")

    forbidden = (
        "label",
        "multiline",
        "titleClassName",
        "timestampClassName",
        "secondaryClassName",
    )
    for name in forbidden:
        if re.search(rf"\b{re.escape(name)}\??:", props):
            failures.append(f"SidebarItemButton must not expose {name}")

required_classes = (
    '"h-5 min-w-0 flex-1 text-sm"',
    'text-xs font-normal text-text-tertiary',
    'text-[11px] leading-4 font-normal text-text-tertiary',
)
for class_contract in required_classes:
    if class_contract not in primitives:
        failures.append(f"shared sidebar typography is missing {class_contract}")

for retired_name in ("SidebarThreadTitleLine.tsx", "SidebarThreadSourceLine.tsx"):
    if (conversation_components / retired_name).exists():
        failures.append(f"retired custom sidebar typography component returned: {retired_name}")

if failures:
    print("Sidebar typography guardrail failed:", file=sys.stderr)
    for failure in failures:
        print(f"  - {failure}", file=sys.stderr)
    raise SystemExit(1)

print("Sidebar typography guardrail passed.")
PY
