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 changeQuery = vi.fn();
    const changeRegionId = vi.fn();
    vi.mocked(useHostBdsPage).mockReturnValue(pageFixture({ changeQuery, changeRegionId }));

    render(<HostBdLeadersPage />);

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

    expect(changeQuery).toHaveBeenCalledWith("164453");
    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 },
        changeParentLeaderUserId: vi.fn(),
        changeQuery: vi.fn(),
        changeRegionId: vi.fn(),
        changeStatus: 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,
        parentLeaderUserId: "",
        query: "",
        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(),
        ...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",
    };
}
