import { fireEvent, render, screen } from "@testing-library/react";
import { beforeEach, expect, test, vi } from "vitest";

const mocks = vi.hoisted(() => ({ openPermissions: vi.fn(), page: null }));

vi.mock("@/features/external-admin-users/hooks/useExternalAdminUsersPage.js", () => ({
    useExternalAdminUsersPage: () => mocks.page,
}));

import { ExternalAdminUsersPage } from "./ExternalAdminUsersPage.jsx";

beforeEach(() => {
    mocks.openPermissions.mockReset();
    mocks.page = pageFixture();
});

test("exposes a dedicated row action for configuring external account permissions", () => {
    render(<ExternalAdminUsersPage />);

    const button = screen.getByRole("button", { name: "配置权限" });
    fireEvent.click(button);

    expect(mocks.openPermissions).toHaveBeenCalledWith(mocks.page.data.items[0]);
    expect(screen.getByText("2 项")).toBeInTheDocument();
});

test("disables permission configuration without the main-admin grant ability", () => {
    mocks.page.abilities.canManagePermissions = false;
    render(<ExternalAdminUsersPage />);

    expect(screen.getByRole("button", { name: "配置权限" })).toBeDisabled();
});

function pageFixture() {
    const item = {
        appCode: "fami",
        createdAtMs: 1784073600000,
        createdByAdminId: 1,
        id: 71,
        linkedUser: { displayUserId: "123456", userId: "user-1", username: "fami123456" },
        permissions: ["user:list", "user:update"],
        permissionRevision: 3,
        status: "active",
        username: "123456",
    };
    return {
        abilities: {
            canCreate: true,
            canManagePermissions: true,
            canResetPassword: true,
            canStatus: true,
            canView: true,
        },
        catalogError: "",
        catalogLoading: false,
        closeCreate: vi.fn(),
        closePassword: vi.fn(),
        closePermissions: vi.fn(),
        createForm: { confirmPassword: "", displayUserId: "", password: "", permissions: [], username: "" },
        createOpen: false,
        data: { items: [item], page: 1, pageSize: 50, total: 1 },
        error: "",
        keyword: "",
        loading: false,
        loadingAction: "",
        lookupLoading: false,
        lookupTarget: vi.fn(),
        openCreate: vi.fn(),
        openPassword: vi.fn(),
        openPermissions: mocks.openPermissions,
        page: 1,
        passwordForm: { confirmPassword: "", password: "" },
        passwordUser: null,
        permissionCatalog: { items: [], total: 0 },
        permissionError: "",
        permissionForm: { expectedRevision: null, permissions: [] },
        permissionLoading: false,
        permissionUser: null,
        reload: vi.fn(),
        reloadPermissionCatalog: vi.fn(),
        resetFilters: vi.fn(),
        retryPermissions: vi.fn(),
        setCreateForm: vi.fn(),
        setDisplayUserId: vi.fn(),
        setKeyword: vi.fn(),
        setPage: vi.fn(),
        setPasswordForm: vi.fn(),
        setPermissions: vi.fn(),
        setStatus: vi.fn(),
        status: "",
        submitCreate: vi.fn(),
        submitPassword: vi.fn(),
        submitPermissions: vi.fn(),
        targetUser: null,
        toggleStatus: vi.fn(),
    };
}
