import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { expect, test, vi } from "vitest";
import { UserFinanceScopeEditor } from "./UserFinanceScopeEditor.jsx";

const catalog = {
  apps: [{ appCode: "aslan", appName: "Aslan" }],
  regions: [
    { appCode: "aslan", name: "阿语区", regionCode: "AR", regionId: 1 },
    { appCode: "aslan", name: "菲律宾", regionCode: "PH", regionId: 2 },
    { appCode: "aslan", name: "土耳其", regionCode: "TR", regionId: 3 }
  ]
};

test("greys out regions held by other users and keeps own/unassigned regions editable", async () => {
  const user = userEvent.setup();
  const onChecked = vi.fn();
  render(
    <UserFinanceScopeEditor
      assignments={[
        { appCode: "aslan", regionId: 1, userId: 9, userName: "王宏力" },
        { appCode: "aslan", regionId: 2, userId: 7, userName: "李浩铭" }
      ]}
      catalog={catalog}
      checkedScopes={[{ appCode: "aslan", regionId: 2 }]}
      excludeUserId={7}
      onChecked={onChecked}
    />
  );

  expect(screen.getByText("已选 1 项 · 他人已分 1 · 未分配 1")).toBeInTheDocument();
  expect(screen.getByText("已分配：王宏力")).toBeInTheDocument();
  expect(screen.getByRole("checkbox", { name: /阿语区/ })).toBeDisabled();

  const ownRegion = screen.getByRole("checkbox", { name: /菲律宾/ });
  expect(ownRegion).toBeChecked();
  expect(ownRegion).not.toBeDisabled();
  await user.click(ownRegion);
  expect(onChecked).toHaveBeenCalledWith("aslan", 2, false);

  await user.click(screen.getByRole("checkbox", { name: /土耳其/ }));
  expect(onChecked).toHaveBeenCalledWith("aslan", 3, true);
});

test("another user holding 全部区域 blocks every region of that app", () => {
  render(
    <UserFinanceScopeEditor
      assignments={[{ appCode: "aslan", regionId: 0, userId: 9, userName: "王宏力" }]}
      catalog={catalog}
      checkedScopes={[]}
      excludeUserId={7}
      onChecked={vi.fn()}
    />
  );

  expect(screen.getByText("已选 0 项 · 他人已分 3 · 未分配 0")).toBeInTheDocument();
  expect(screen.getByRole("checkbox", { name: /全部区域/ })).toBeDisabled();
  expect(screen.getByRole("checkbox", { name: /阿语区/ })).toBeDisabled();
  expect(screen.getByRole("checkbox", { name: /菲律宾/ })).toBeDisabled();
  expect(screen.getByRole("checkbox", { name: /土耳其/ })).toBeDisabled();
});

test("仅看未分配 filter keeps only regions nobody holds", async () => {
  const user = userEvent.setup();
  render(
    <UserFinanceScopeEditor
      assignments={[{ appCode: "aslan", regionId: 1, userId: 9, userName: "王宏力" }]}
      catalog={catalog}
      checkedScopes={[{ appCode: "aslan", regionId: 2 }]}
      excludeUserId={7}
      onChecked={vi.fn()}
    />
  );

  await user.click(screen.getByRole("checkbox", { name: "仅看未分配" }));

  expect(screen.queryByRole("checkbox", { name: /全部区域/ })).not.toBeInTheDocument();
  expect(screen.queryByRole("checkbox", { name: /阿语区/ })).not.toBeInTheDocument();
  expect(screen.queryByRole("checkbox", { name: /菲律宾/ })).not.toBeInTheDocument();
  expect(screen.getByRole("checkbox", { name: /土耳其/ })).toBeInTheDocument();
});
