import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Local ACP stays available after saving custom model metadata", WEB, async ({ t3, app, screen, browser }) => {
  // Mask machine-local executable paths and the temporary pairing credential in public evidence.
  await browser.addInitScript(() => {
    document.addEventListener("DOMContentLoaded", () => {
      const style = document.createElement("style");
      const inputs = location.pathname === "/pair"
        ? "input"
        : 'input[id$="-binaryPath"], input[id$="-commandPath"], input[aria-label^="Argument "]';
      style.textContent = `${inputs} { color: transparent !important; background: repeating-linear-gradient(45deg,#ddd,#ddd 3px,#eee 3px,#eee 6px) !important; }`;
      document.head.append(style);
    });
  });
  const project = await t3.project("ACP model demo");
  await t3.thread({ project, title: "Working local ACP", messages: ["Confirm the replay provider works"] });
  await t3.open("/settings/providers");
  await screen.getByRole("button", "Select Replay (dev)", { exact: true }).tap();
  await app.screenshot("provider-initially-working");
  await screen.getByRole("button", "Add custom model", { exact: true }).tap();
  await browser.locator('#provider-instance-devReplay-custom-model').fill("custom-evidence-model");
  await screen.getByRole("button", "Add", { exact: true }).tap();
  await screen.getByRole("button", "Edit custom-evidence-model", { exact: true }).tap();
  await screen.getByRole("textbox", "Display name", { exact: true }).fill("Evidence Custom Model");
  await screen.getByRole("button", "Custom option", { exact: true }).tap();
  await screen.getByRole("textbox", "Option id", { exact: true }).fill("quality");
  await screen.getByRole("textbox", "Option label", { exact: true }).fill("Quality");
  await screen.getByRole("button", "Add choice", { exact: true }).tap();
  await screen.getByRole("textbox", "Choice value", { exact: true }).fill("high");
  await screen.getByRole("textbox", "Choice label", { exact: true }).fill("High");
  await app.screenshot("custom-model-draft");
  await screen.getByRole("button", "Save", { exact: true }).tap();
  await screen.getByRole("button", "Refresh provider status", { exact: false }).tap();
  await app.open("/settings/providers");
  await screen.getByRole("button", "Select Replay (dev)", { exact: true }).tap();
  await app.screenshot("provider-after-reload");

  await expect(browser.locator('[data-provider-detail]')).toContainText("Evidence Custom Model");
  await screen.getByRole("button", "Edit custom-evidence-model", { exact: true }).tap();
  await app.screenshot("retained-model-metadata");
  await expect(screen.getByRole("textbox", "Display name", { exact: true })).toHaveValue("Evidence Custom Model");
  await expect(screen.getByRole("textbox", "Option label", { exact: true })).toHaveValue("Quality");
  await expect(screen.getByRole("textbox", "Choice value", { exact: true })).toHaveValue("high");
  await expect(screen.getByRole("textbox", "Choice label", { exact: true })).toHaveValue("High");
  await expect(browser.locator('[data-provider-detail]')).not.toContainText("Invalid config");
  await expect(browser.locator('[data-provider-detail]')).not.toContainText("Disabled");
});
