import test from "node:test";
import assert from "node:assert/strict";
import {
  approvalPolicyLabel,
  closeComposerModelMenu,
  closeComposerPermissionMenu,
  effectiveEffort,
  modelButtonLabel,
  permissionLabel,
  requiresFullAccessConfirmation,
  reasoningEffortOptions,
  selectablePermissionProfiles,
  selectedOrDefaultModel
} from "./composer-menus.js";
import { translate } from "../../shared/i18n.js";

const models = [
  { id: "gpt-5.4", model: "gpt-5.4", displayName: "GPT-5.4", isDefault: false, defaultReasoningEffort: "high" },
  { id: "gpt-5.5", model: "gpt-5.5", displayName: "GPT-5.5", isDefault: true, defaultReasoningEffort: "medium" }
];

test("model button uses selected or actual default model and effective effort", () => {
  assert.equal(selectedOrDefaultModel(models, "").model, "gpt-5.5");
  assert.equal(selectedOrDefaultModel(models, "gpt-5.4").model, "gpt-5.4");
  assert.equal(effectiveEffort(models[1], ""), "medium");
  assert.equal(modelButtonLabel(models, { model: "", effort: "" }), "GPT-5.5 · Medium");
  assert.equal(modelButtonLabel(models, { model: "gpt-5.4", effort: "xhigh" }), "GPT-5.4 · Extra high");
});

test("reasoning effort options follow selected model capabilities", () => {
  const terra = {
    reasoningEfforts: [
      { id: "low", description: "Fast responses" },
      { id: "max", description: "Maximum reasoning depth" },
      { id: "ultra", description: "Maximum reasoning with automatic task delegation" }
    ]
  };

  assert.deepEqual(reasoningEffortOptions(terra), [
    { id: "low", label: "Light", description: "Fast responses" },
    { id: "max", label: "Max", description: "Maximum reasoning depth" },
    { id: "ultra", label: "Ultra", description: "Maximum reasoning with automatic task delegation" }
  ]);
});

test("each model exposes only its own live reasoning efforts", () => {
  const terra = { model: "gpt-5.6-terra", reasoningEfforts: [{ id: "low" }, { id: "medium" }, { id: "high" }, { id: "xhigh" }, { id: "max" }, { id: "ultra" }] };
  const gpt55 = { model: "gpt-5.5", reasoningEfforts: [{ id: "low" }, { id: "medium" }, { id: "high" }, { id: "xhigh" }] };

  assert.deepEqual(reasoningEffortOptions(terra).map(({ id }) => id), ["low", "medium", "high", "xhigh", "max", "ultra"]);
  assert.deepEqual(reasoningEffortOptions(gpt55).map(({ id }) => id), ["low", "medium", "high", "xhigh"]);
});

test("reasoning effort never invents options absent from app-server capability", () => {
  assert.deepEqual(reasoningEffortOptions({ defaultReasoningEffort: "medium" }), []);
});

test("reasoning effort labels stay English in the Korean UI", () => {
  assert.equal(translate("en", "composer.effort.low"), "Light");
  assert.equal(translate("ko", "composer.effort.low"), "Light");
  assert.equal(translate("ko", "composer.effort.medium"), "Medium");
  assert.equal(translate("ko", "composer.effort.high"), "High");
  assert.equal(translate("ko", "composer.effort.xhigh"), "Extra high");
  assert.equal(translate("ko", "composer.effort.max"), "Max");
  assert.equal(translate("ko", "composer.effort.ultra"), "Ultra");
});

test("permission labels remain translated in English and Korean", () => {
  assert.equal(translate("en", "composer.permission.fullAccess"), "Full access");
  assert.equal(translate("ko", "composer.permission.fullAccess"), "전체 액세스");
});

test("selecting model option closes both model menus", () => {
  const menu = { open: true };
  const modelPicker = { open: true };
  closeComposerModelMenu(menu, modelPicker);
  assert.equal(menu.open, false);
  assert.equal(modelPicker.open, false);
});

test("selecting permission option closes menu", () => {
  const menu = { open: true };
  closeComposerPermissionMenu(menu);
  assert.equal(menu.open, false);
});

test("only a transition into full access requires confirmation", () => {
  assert.equal(requiresFullAccessConfirmation(":workspace", ":danger-full-access"), true);
  assert.equal(requiresFullAccessConfirmation(":read-only", ":danger-full-access"), true);
  assert.equal(requiresFullAccessConfirmation(":danger-full-access", ":danger-full-access"), false);
  assert.equal(requiresFullAccessConfirmation(":danger-full-access", ":workspace"), false);
});

test("permission menu uses allowed app-server profiles and desktop labels", () => {
  const profiles = [
    { id: ":read-only", allowed: true },
    { id: ":workspace", allowed: true },
    { id: ":danger-full-access", allowed: true },
    { id: "managed", allowed: false, description: "Not allowed" },
    { id: "project-edit", allowed: true, description: "Write app files" }
  ];

  assert.deepEqual(selectablePermissionProfiles(profiles).map((profile) => profile.id), [":read-only", ":workspace", ":danger-full-access", "project-edit"]);
  assert.equal(permissionLabel(profiles, ":workspace"), "Workspace");
  assert.equal(permissionLabel(profiles, ":danger-full-access"), "Full access");
  assert.equal(permissionLabel(profiles, "project-edit"), "project-edit");
});

test("approval policy labels describe the four supported review modes", () => {
  assert.equal(approvalPolicyLabel("untrusted"), "Always ask");
  assert.equal(approvalPolicyLabel("on-failure"), "Ask after failure");
  assert.equal(approvalPolicyLabel("on-request"), "Ask when needed");
  assert.equal(approvalPolicyLabel("never"), "Never ask");
});
