#!/usr/bin/env python3

from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
UI = (ROOT / "ui" / "settings.html").read_text(encoding="utf-8")
SKILL = (ROOT / "skills" / "carbon-settings" / "SKILL.md").read_text(encoding="utf-8")
BROWSER_SKILL = (ROOT / "skills" / "carbon-browser" / "SKILL.md").read_text(encoding="utf-8")
SERVER = (ROOT / "server" / "index.js").read_text(encoding="utf-8")


def require(condition: bool, message: str) -> None:
    if not condition:
        raise SystemExit(f"FAIL: {message}")


require('name="color-scheme" content="dark"' in UI, "settings page is not dark Carbon branded")
require('aria-hidden="true"><svg viewBox="0 0 256 256"' in UI, "settings page is missing the Carbon cut-C mark")
require('aria-hidden="true">T</div>' not in UI, "settings page retained the deprecated T badge")
require('id="browser-controller"' in UI, "preferred browser-control selector is missing")
require('id="output-verbosity"' in UI, "output verbosity selector is missing")
require('<option value="verbose">Verbose · default' in UI, "verbose output is not the visible default")
require("Every mode keeps the generated HTML report complete" in UI, "verbosity setting does not preserve complete reports")
require(UI.count('<input type="checkbox" name="browser-controller-allowed"') == 5, "settings page must expose all five local browser controls")
for controller in (
    "host-isolated",
    "user-chrome",
    "vibium-webdriver-bidi",
    "playwright-cdp",
    "selenium-webdriver",
):
    require(f'value="{controller}"' in UI, f"settings page is missing {controller}")
require("intentionally off by default" in UI, "live Chrome opt-in warning is missing")
require("Every invocation of this settings command must call `carbon_open_settings`" in SKILL, "settings workflow does not always open the page")
require("Never use `user-chrome` unless it is explicitly allowed" in BROWSER_SKILL, "browser workflow lacks live-Chrome consent enforcement")
require('"host-isolated",\n  "vibium-webdriver-bidi"' in SERVER, "safe default controller allowlist is missing")
require("browserControllerAllowlist" in SERVER and "userChromeExplicitOptIn" in SERVER, "runtime does not publish browser-control settings")
require('outputVerbositySetting(value, fallback = "verbose")' in SERVER, "runtime does not default output verbosity to verbose")
require('enum: ["verbose", "standard", "compact"]' in SERVER, "runtime does not expose supported verbosity choices")
require('href="https://reports.testers.ai/signup?brand=testers.ai"' in UI, "verified testers.ai account-creation link is missing")
require('href="https://reports.testers.ai/dashboard"' in UI, "verified testers.ai dashboard link is missing")
require("Generate CI API key" in UI and "Paste it here and save settings" in UI, "API-key onboarding steps are incomplete")
require("Never paste an API key into chat" in UI, "API-key secret handling warning is missing")
require("TESTERS_SIGNUP_URL" in SERVER and "cloudAccountSetup" in SERVER, "runtime does not return safe cloud account setup guidance")
for identifier in ("ai-provider", "ai-model", "ai-api-key", "ai-key-state", "clear-ai-key"):
    require(f'id="{identifier}"' in UI, f"AI screenshot review setting is missing: {identifier}")
require("The selected screenshot and short review focus are still sent to this model provider" in UI, "AI screenshot transmission boundary is missing")
require("never written into test source" in UI, "AI key source-code boundary is missing")
require("AI_PROVIDER_ENV" in SERVER and "aiApiKeyConfigured" in SERVER, "runtime does not expose safe local AI readiness metadata")
for identifier in (
    "integration-section", "jira-base-url", "jira-api-token", "xray-client-id", "xray-client-secret",
    "testrail-base-url", "testrail-api-key", "testrail-project-id", "integration-suggestions-enabled",
):
    require(f'id="{identifier}"' in UI, f"external quality integration setting is missing: {identifier}")
require("Read first. Sync only after a reviewed preview." in UI, "read-first integration boundary is missing")
require("export or back up" in UI and "does not support deletion" in UI, "external sync backup/delete warning is incomplete")
require("INTEGRATION_CREDENTIALS" in SERVER and "safeIntegrationStatus" in SERVER, "runtime does not protect and redact integration credentials")
require("backupConfirmed" in SERVER and "The sync preview changed after review" in SERVER, "runtime lacks guarded digest/backup sync enforcement")
require('id="ads-enabled"' in UI and "On · contextual and frequency-capped" in UI and "Off · hide all optional promotions" in UI, "settings page lacks an explicit ads On/Off control")
require("never shows more than one per report" in UI and "24 hours" in UI and "seven days" in UI, "settings page does not explain contextual frequency limits")
require("adsEnabled" in SERVER and "globalCooldownHours" in SERVER and "sameOfferCooldownDays" in SERVER, "runtime does not expose the ads switch and frequency policy")

print("PASS: always-open Carbon settings UI, verbose-by-default output, explicit contextual ads On/Off control, browser controls, secure cloud/model/integration onboarding, read-first external evidence, and guarded sync enforcement verified.")
