import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";

const app = readFileSync("src/app/App.tsx", "utf8");
const bindingModal = readFileSync(
  "src/features/bindings/AttachmentBindingModal.tsx",
  "utf8",
);
const unbindModal = readFileSync(
  "src/features/bindings/UnbindAttachmentModal.tsx",
  "utf8",
);
const permissionModal = readFileSync(
  "src/features/permissions/PermissionModal.tsx",
  "utf8",
);

describe("F6 UI scope and safety", () => {
  it("gates management entry points by allowedActions", () => {
    expect(app).toContain("AllowedAction.BIND_ATTACHMENT");
    expect(app).toContain("AllowedAction.UNBIND_ATTACHMENT");
    expect(app).toContain("AllowedAction.CONFIG_PERMISSION");
    expect(app).not.toMatch(/roleCode[^]*BIND_ATTACHMENT/);
  });

  it("marks existing bindings and supports preserved multi-selection", () => {
    expect(bindingModal).toContain("已挂载");
    expect(bindingModal).toContain("selected");
    expect(bindingModal).toContain("toggleAttachmentSelection");
    expect(bindingModal).toContain("翻页和搜索不会清除选择");
  });

  it("loads the latest main plan version before bind and unbind", () => {
    expect(bindingModal).toContain("documentApi.detail(mainPlan.id)");
    expect(bindingModal).toContain("mainPlanRowVersion: latest.rowVersion");
    expect(unbindModal).toContain("documentApi.detail(mainPlan.id)");
    expect(unbindModal).toContain("latest.rowVersion");
  });

  it("clearly states unbinding never deletes attachment data", () => {
    expect(unbindModal).toContain("附件及其文件不会被删除");
    expect(unbindModal).not.toContain(">删除附件<");
  });

  it("keeps conflict drafts and does not auto-retry writes", () => {
    expect(bindingModal).toContain("请核对保留的选择后再次提交");
    expect(permissionModal).toContain("setDraft((current)");
    expect(permissionModal).toContain("loadPermissions(true)");
    expect(permissionModal.match(/permissionApi\.save/g)).toHaveLength(1);
    expect(unbindModal.match(/bindingApi\.unbind/g)).toHaveLength(1);
  });

  it("shows inherited sub-plan permissions without a save path", () => {
    expect(permissionModal).toContain("子方案权限继承所属主案，不能单独配置");
    expect(permissionModal).toContain("!editable || detail.inherited");
    expect(permissionModal).toContain("if (!draft || saving || !editable || isSubPlan || isAttachment) return");
  });

  it("使用面向用户的共享附件权限说明", () => {
    expect(permissionModal).toContain("共享附件对所有已登录用户可见");
    expect(permissionModal).not.toContain("不配置组织或人员 ACL");
    expect(app).not.toContain("F6 挂载关系与主案权限已接入");
  });

  it("uses real organization and user directory hooks", () => {
    expect(permissionModal).toContain("useOrganizationTree");
    expect(permissionModal).toContain("useUserDirectory");
    expect(permissionModal).toContain("includeDescendants");
    expect(permissionModal).toContain("organizationId");
  });

  it("contains all five explicit boolean actions", () => {
    for (const action of [
      "canView",
      "canDownload",
      "canEdit",
      "canManagePermission",
      "canDelete",
    ]) {
      expect(permissionModal).toContain(action);
    }
  });

  it("does not call F7, audit, statistics, restore, or deprecated file routes", () => {
    const f6Sources = [
      bindingModal,
      unbindModal,
      permissionModal,
      readFileSync("src/features/bindings/bindingApi.ts", "utf8"),
      readFileSync("src/features/permissions/permissionApi.ts", "utf8"),
    ].join("\n");
    expect(f6Sources).not.toContain("/restore");
    expect(f6Sources).not.toContain("audit/logs");
    expect(f6Sources).not.toContain("statistics/");
    expect(f6Sources).not.toMatch(/attachments\/\$\{[^}]+\}\/(preview|download)/);
  });
});
