import { fireEvent, render, screen } from "@testing-library/react";
import { afterEach, expect, test, vi } from "vitest";
import { HostBdLeadersPage } from "./HostBdLeadersPage.jsx";
import { useHostBdsPage } from "@/features/host-org/hooks/useHostBdsPage.js";

vi.mock("@/features/host-org/hooks/useHostBdsPage.js", () => ({
    useHostBdsPage: vi.fn(),
}));

afterEach(() => {
    vi.clearAllMocks();
});

test("bd leader table exposes user and region column filters", () => {
    const changeUserFilter = vi.fn();
    const changeRegionId = vi.fn();
    vi.mocked(useHostBdsPage).mockReturnValue(pageFixture({ changeUserFilter, changeRegionId }));

    render(<HostBdLeadersPage />);

    fireEvent.click(screen.getByRole("button", { name: "用户" }));
    fireEvent.change(screen.getByPlaceholderText("输入短 ID"), {
        target: { value: "164453" },
    });
    fireEvent.click(screen.getByRole("button", { name: "搜索" }));
    fireEvent.click(screen.getByRole("button", { name: "区域" }));
    fireEvent.click(screen.getByRole("menuitem", { name: "南亚区" }));

    expect(changeUserFilter).toHaveBeenCalledWith({ displayUserId: "164453", userId: "", username: "" });
    expect(changeRegionId).toHaveBeenCalledWith("1");
});

function pageFixture(patch = {}) {
    return {
        abilities: {
            canCreate: false,
            canUpdate: false,
            canView: true,
        },
        activeAction: "",
        bdForm: { parentLeaderUserId: "", reason: "", targetUserId: "" },
        bdLeaderForm: { positionAlias: "", reason: "", targetUserId: "" },
        bdUserLookup: { error: "", loading: false, user: null },
        changeParentLeaderFilter: vi.fn(),
        changeRegionId: vi.fn(),
        changeStatus: vi.fn(),
        changeUserFilter: vi.fn(),
        closeAction: vi.fn(),
        data: { items: [leaderFixture()], page: 1, pageSize: 50, total: 1 },
        error: null,
        leaderBDs: { items: [], page: 1, pageSize: 50, total: 0 },
        leaderBDsError: "",
        leaderBDsLoading: false,
        loadLeaderBDs: vi.fn(),
        loading: false,
        loadingAction: "",
        loadingRegions: false,
        openAddLeaderBD: vi.fn(),
        openBDLeaderForm: vi.fn(),
        openLeaderBDs: vi.fn(),
        page: 1,
        parentLeaderFilter: { displayUserId: "", userId: "", username: "" },
        regionId: "",
        regionOptions: [{ label: "南亚区", value: "1" }],
        reload: vi.fn(),
        removeBDLeader: vi.fn(),
        resetFilters: vi.fn(),
        saveBDLeaderPositionAlias: vi.fn(),
        selectedLeader: null,
        setBDForm: vi.fn(),
        setBDLeaderForm: vi.fn(),
        setPage: vi.fn(),
        status: "",
        submitBDLeader: vi.fn(),
        submitLeaderBD: vi.fn(),
        toggleBDLeader: vi.fn(),
        userFilter: { displayUserId: "", userId: "", username: "" },
        ...patch,
    };
}

function leaderFixture() {
    return {
        avatar: "",
        createdAtMs: 1760000000000,
        createdByDisplayUserId: "164000",
        createdByUserId: "1000",
        createdByUsername: "hyappadmin",
        displayUserId: "164453",
        positionAlias: "admin",
        regionId: "1",
        regionName: "南亚区",
        status: "active",
        subBdCount: 0,
        userId: "3001",
        username: "ABER KING",
    };
}
