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

const mocks = vi.hoisted(() => ({
    toggleManager: vi.fn(),
}));

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

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

test("manager status column uses a switch and toggles through the page hook", () => {
    const manager = managerFixture();
    vi.mocked(useHostManagersPage).mockReturnValue(
        pageFixture({ data: { items: [manager], page: 1, pageSize: 50, total: 1 } }),
    );

    render(<HostManagersPage />);

    expect(screen.getByText("VIP")).toBeInTheDocument();

    const statusSwitch = screen.getByRole("switch", { name: "停用经理" });
    expect(statusSwitch).toBeChecked();

    fireEvent.click(statusSwitch);

    expect(mocks.toggleManager).toHaveBeenCalledWith(manager, false);
});

test("manager form only shows the cross-region manager switch when backend policy allows configuration", () => {
    vi.mocked(useHostManagersPage).mockReturnValue(
        pageFixture({
            activeAction: "manager",
            managerForm: {
                ...pageFixture().managerForm,
                managerCrossRegionConfigurable: true,
                canManageCrossRegion: false,
            },
        }),
    );

    const { rerender } = render(<HostManagersPage />);
    expect(screen.getByRole("switch", { name: "跨区经理" })).not.toBeChecked();

    vi.mocked(useHostManagersPage).mockReturnValue(
        pageFixture({
            activeAction: "manager",
            managerForm: {
                ...pageFixture().managerForm,
                managerCrossRegionConfigurable: false,
                canManageCrossRegion: false,
            },
        }),
    );
    rerender(<HostManagersPage />);
    expect(screen.queryByRole("switch", { name: "跨区经理" })).not.toBeInTheDocument();
});

function pageFixture(patch = {}) {
    return {
        abilities: {
            canCreate: true,
            canUpdate: true,
            canView: true,
        },
        activeAction: "",
        changeQuery: vi.fn(),
        changeRegionId: vi.fn(),
        changeStatus: vi.fn(),
        closeAction: vi.fn(),
        data: { items: [], page: 1, pageSize: 50, total: 0 },
        editingManager: null,
        error: null,
        loading: false,
        loadingAction: "",
        loadingRegions: false,
        managerForm: {
            managerCrossRegionConfigurable: true,
            canAddAdmin: true,
            canAddBdLeader: true,
            canManageCrossRegion: false,
            canAddSuperadmin: true,
            canBlockUser: true,
            canGrantAvatarFrame: true,
            canGrantBadge: true,
            canGrantVehicle: true,
            canGrantVip: true,
            canTransferUserCountry: true,
            canUpdateUserLevel: true,
            contact: "",
            targetUserId: "",
        },
        openManagerEdit: vi.fn(),
        openManagerForm: vi.fn(),
        page: 1,
        query: "",
        regionId: "",
        regionOptions: [],
        reload: vi.fn(),
        resetFilters: vi.fn(),
        setManagerForm: vi.fn(),
        setPage: vi.fn(),
        status: "",
        submitManager: vi.fn(),
        toggleManager: mocks.toggleManager,
        ...patch,
    };
}

function managerFixture() {
    return {
        avatar: "",
        bdLeaderCount: 3,
        canAddAdmin: true,
        canAddBdLeader: false,
        canManageCrossRegion: false,
        managerCrossRegionConfigurable: true,
        canAddSuperadmin: true,
        canBlockUser: true,
        canGrantAvatarFrame: true,
        canGrantBadge: true,
        canGrantVehicle: true,
        canGrantVip: true,
        canTransferUserCountry: true,
        canUpdateUserLevel: true,
        contact: "+63 910 323 3670",
        displayUserId: "167579",
        lastInvitedAtMs: 1782198308000,
        regionId: 1,
        regionName: "欧亚美区",
        status: "active",
        updatedAtMs: 1782284840000,
        userId: "10001",
        username: "PH-MANAGER",
    };
}
