import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";

test("native onboarding and device authentication gate the app", async () => {
  const [app, gate, authGate, settings] = await Promise.all([
    readFile(new URL("../../App.jsx", import.meta.url), "utf8"),
    readFile(new URL("./NativeConnectionGate.jsx", import.meta.url), "utf8"),
    readFile(new URL("../auth/AuthenticationGate.jsx", import.meta.url), "utf8"),
    readFile(new URL("../context/SettingsWindow.jsx", import.meta.url), "utf8")
  ]);

  assert.match(app, /<NativeConnectionGate><AuthenticationGate><App \/><\/AuthenticationGate><\/NativeConnectionGate>/);
  assert.match(gate, /saveConnection\(address\)/);
  assert.match(gate, /verifyServerConnection\(candidate\)/);
  assert.match(gate, /scanConnectionSetupQr/);
  assert.match(gate, /pairDeviceAtOrigin/);
  assert.match(gate, /t\("connection\.scanQr"\)/);
  assert.match(gate, /useBackLayer\(scanning, \(\) => setScanning\(false\), "qr-scanner"\)/);
  assert.match(gate, /writeSavedServerOrigin\(undefined, origin\)/);
  assert.match(authGate, /scanDevicePairingQr/);
  assert.match(authGate, /pairDevice/);
  assert.match(authGate, /pairingAttemptId/);
  assert.match(authGate, /status\.required === false[\s\S]*loadAuthenticationSession\(\)[\s\S]*setState\("ready"\)/);
  assert.match(authGate, /async function continueDevelopment\(\)[\s\S]*await loadAuthenticationStatus\(\)[\s\S]*setState\("ready"\)/);
  assert.match(authGate, /continueDevelopment[\s\S]*setError\("pc_unreachable"\)/);
  assert.match(authGate, /import\.meta\.env\.DEV && <button className="connection-dev-bypass"/);
  assert.match(authGate, /onClick=\{continueDevelopment\}/);
  assert.match(authGate, /\{language === "ko" \? "개발자 모드로 계속" : "Continue in developer mode"\}<\/button>/);
  assert.doesNotMatch(authGate, /pairing\.continueDevelopment/);
  assert.match(settings, /platformRuntime\.isNative/);
  assert.match(settings, /readSavedServerOrigin\(\)/);
  assert.match(settings, /requestPcConnectionChange\(\)/);
  assert.match(settings, /location\.reload\(\)/);
});

test("PC connection changes can be cancelled with the screen or Android back action", async () => {
  const [gate, settings] = await Promise.all([
    readFile(new URL("./NativeConnectionGate.jsx", import.meta.url), "utf8"),
    readFile(new URL("../context/SettingsWindow.jsx", import.meta.url), "utf8")
  ]);

  assert.match(settings, /requestPcConnectionChange\(\)/);
  assert.match(settings, /function changePc\(\) \{\s*requestPcConnectionChange\(\);\s*globalThis\.location\.reload\(\);\s*\}/);
  assert.match(gate, /useBackLayer\(changeRequested, cancelChangePc, "connection-change"\)/);
  assert.match(gate, /className="[^"]*connection-back"[^>]*onClick=\{cancelChangePc\}/);
  assert.match(gate, /clearPcConnectionChangeRequest\(\)/);
});

test("PC management can remove the saved PC after confirmation", async () => {
  const settings = await readFile(new URL("../context/SettingsWindow.jsx", import.meta.url), "utf8");

  assert.match(settings, /t\("settings\.pcManagement"\)/);
  assert.match(settings, /globalThis\.confirm\(t\("settings\.removePcConfirm"\)\)/);
  assert.match(settings, /disconnectPcConnection\(\)/);
  assert.match(settings, /t\("settings\.removePc"\)/);
});

test("PC management card stays light in light mode", async () => {
  const css = await readFile(new URL("../context/context.css", import.meta.url), "utf8");

  assert.match(css, /html\[data-theme="light"\] \.app-shell \.settings-connection\{[^}]*background(?:-color)?:#fff!important/s);
});

test("PC management shows the PC name and recent connection status", async () => {
  const settings = await readFile(new URL("../context/SettingsWindow.jsx", import.meta.url), "utf8");

  assert.match(settings, /fetchPcStatus\(savedPcOrigin\)/);
  assert.match(settings, /t\("settings\.pcOnline"\)/);
  assert.match(settings, /t\("settings\.pcLastChecked"/);
});

test("QR connection screen can switch and persist Korean or English before onboarding", async () => {
  const gate = await readFile(new URL("./NativeConnectionGate.jsx", import.meta.url), "utf8");

  assert.match(gate, /const \{ language, setLanguage, t \} = useI18n\(\)/);
  assert.match(gate, /setLanguage\("ko"\)/);
  assert.match(gate, /setLanguage\("en"\)/);
  assert.match(gate, /aria-pressed=\{language === "ko"\}/);
  assert.match(gate, /aria-pressed=\{language === "en"\}/);
});
