import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { purchaseSheetModel } from "../pro/pro-view-model.js";
import { LEGAL_SUPPORT_LINKS, PRODUCT_CONTACT } from "./legal-support.js";

test("settings exposes the app-specific contact and every official public document", async () => {
  const [config, section, settings, messages, css] = await Promise.all([
    readFile(new URL("./legal-support.js", import.meta.url), "utf8"),
    readFile(new URL("./LegalSupportSection.jsx", import.meta.url), "utf8"),
    readFile(new URL("./SettingsWindow.jsx", import.meta.url), "utf8"),
    readFile(new URL("../../shared/i18n.js", import.meta.url), "utf8"),
    readFile(new URL("./context.css", import.meta.url), "utf8")
  ]);

  assert.match(config, /https:\/\/coderemotemanager\.makecdev\.com/);
  assert.match(config, /coderemote@makecdev\.com/);
  assert.match(config, /developer:\s*"MakeCdev"/);
  assert.doesNotMatch(config, /operator:\s*"MakeCdev"/);
  for (const route of ["privacy", "terms", "support", "delete-account", "licenses"]) {
    assert.match(config, new RegExp(`/${route}`));
  }
  assert.match(section, /mailto:\$\{PRODUCT_CONTACT\.email\}/);
  assert.match(section, /Browser\.open\(\{ url \}\)/);
  assert.match(section, /rel="noreferrer"/);
  assert.match(section, /useOutsideDismiss\(controlRef, closeMenu, menuOpen/);
  assert.match(section, /className="[^"]*settings-legal-trigger[^"]*"/);
  assert.match(section, /aria-haspopup="menu"/);
  assert.match(section, /aria-expanded=\{menuOpen\}/);
  assert.match(section, /menuOpen && <div className="settings-legal-menu" role="menu"/);
  assert.match(section, /closeMenu\(\);[\s\S]*Browser\.open\(\{ url \}\)/);
  assert.match(css, /\.settings-legal-trigger\{[^}]*min-height:48px/);
  assert.match(css, /\.settings-legal-menu\{[^}]*position:absolute[^}]*max-height:/);
  assert.match(settings, /<LegalSupportSection \/>/);
  assert.match(messages, /settings\.legalSupport/);
  assert.match(messages, /settings\.officialWebsite/);
  assert.match(messages, /settings\.contactEmail/);
  assert.match(messages, /"settings\.dataDeletion": "Data deletion"/);
  assert.match(messages, /"settings\.dataDeletion": "데이터 삭제"/);
});

test("the in-app Pro purchase disclosure matches the public one-time purchase boundary", async () => {
  const [sheet, settings, accountManagement] = await Promise.all([
    readFile(new URL("../pro/ProPurchaseSheet.jsx", import.meta.url), "utf8"),
    readFile(new URL("./SettingsWindow.jsx", import.meta.url), "utf8"),
    readFile(new URL("../accounts/AccountManagementPage.jsx", import.meta.url), "utf8")
  ]);
  const ko = purchaseSheetModel({ language: "ko" });
  const en = purchaseSheetModel({ language: "en" });

  assert.match(settings, /<AccountManagementPage/);
  assert.match(accountManagement, /<ProSettingsSection/);
  assert.match(settings, /<LegalSupportSection \/>/);
  assert.match(sheet, /model\.description/);
  assert.match(ko.description, /모든 원격 기능.*계정 1개.*Pro.*5개/s);
  assert.match(ko.description, /비소모성.*일회성 구매.*Google Play.*구매 복원/s);
  assert.match(en.description, /all remote features.*one account.*Pro.*five accounts/is);
  assert.match(en.description, /one-time non-consumable purchase.*Google Play.*restore/is);
  assert.doesNotMatch(`${ko.description}\n${en.description}`, /Codex (?:사용권|구독)(?:이|은)? (?:포함됩니다|함께 제공됩니다)|Codex (?:access|subscription) (?:is )?included/is);
  for (const route of ["privacy", "terms", "support", "delete-account"]) {
    assert.equal(LEGAL_SUPPORT_LINKS.find(link => link.id === route)?.url, `${PRODUCT_CONTACT.siteOrigin}/${route}`);
  }
});

test("settings exposes the common policy effective date", async () => {
  const [config, section, messages] = await Promise.all([
    readFile(new URL("./legal-support.js", import.meta.url), "utf8"),
    readFile(new URL("./LegalSupportSection.jsx", import.meta.url), "utf8"),
    readFile(new URL("../../shared/i18n.js", import.meta.url), "utf8")
  ]);

  assert.match(config, /policyEffectiveDate:\s*"2026-08-02"/);
  assert.match(section, /t\("settings\.policyEffectiveDate"\)[^\n]*PRODUCT_CONTACT\.policyEffectiveDate/);
  assert.match(messages, /"settings\.policyEffectiveDate": "Policy effective date"/);
  assert.match(messages, /"settings\.policyEffectiveDate": "정책 시행일"/);
});
