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

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

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

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

test("sub coin seller application page renders pending applications with audit actions", () => {
    const application = applicationFixture();
    vi.mocked(useHostSubCoinSellerApplicationsPage).mockReturnValue(pageFixture({ data: pageData([application]) }));

    render(<HostSubCoinSellerApplicationsPage />);

    expect(screen.getByText("父币商")).toBeInTheDocument();
    expect(screen.getByText("申请子币商")).toBeInTheDocument();
    expect(screen.getByText("Parent seller")).toBeInTheDocument();
    expect(screen.getByText("Target user")).toBeInTheDocument();

    fireEvent.click(screen.getAllByLabelText("通过").find((node) => node.tagName.toLowerCase() === "button"));
    fireEvent.click(screen.getAllByLabelText("拒绝").find((node) => node.tagName.toLowerCase() === "button"));

    expect(mocks.openReview).toHaveBeenCalledWith(application, "approved");
    expect(mocks.openReview).toHaveBeenCalledWith(application, "rejected");
});

test("sub coin seller application page hides audit actions for reviewed rows", () => {
    vi.mocked(useHostSubCoinSellerApplicationsPage).mockReturnValue(
        pageFixture({ data: pageData([applicationFixture({ status: "approved" })]) }),
    );

    render(<HostSubCoinSellerApplicationsPage />);

    expect(screen.queryByLabelText("通过")).not.toBeInTheDocument();
    expect(screen.queryByLabelText("拒绝")).not.toBeInTheDocument();
});

function pageFixture(patch = {}) {
    return {
        abilities: { canAudit: true, canView: true },
        activeAction: "",
        changeKeyword: vi.fn(),
        changeParentUserId: vi.fn(),
        changeStatus: vi.fn(),
        changeTargetUserId: vi.fn(),
        closeAction: vi.fn(),
        data: pageData([applicationFixture()]),
        error: null,
        keyword: "",
        loading: false,
        loadingAction: "",
        openReview: mocks.openReview,
        page: 1,
        parentUserId: "",
        resetFilters: mocks.resetFilters,
        reviewDecision: "approved",
        reviewForm: { commandId: "coin-seller-sub-approved-test", reason: "" },
        selectedApplication: null,
        setPage: vi.fn(),
        setReviewForm: vi.fn(),
        status: "pending",
        submitReview: vi.fn((event) => event.preventDefault()),
        targetUserId: "",
        ...patch,
    };
}

function pageData(items) {
    return { items, page: 1, pageSize: 50, total: items.length };
}

function applicationFixture(patch = {}) {
    return {
        applicationId: "sub-app-1",
        createdAtMs: 1760000000000,
        parent: {
            avatar: "",
            displayUserId: "163001",
            regionName: "中东区",
            userId: "3001",
            username: "Parent seller",
        },
        parentUserId: "3001",
        relationId: "",
        reviewReason: "",
        reviewedAtMs: 0,
        reviewedByAdminId: "",
        status: "pending",
        target: {
            avatar: "",
            displayUserId: "168933",
            regionName: "中东区",
            userId: "3002",
            username: "Target user",
        },
        targetUserId: "3002",
        updatedAtMs: 1760000000000,
        ...patch,
    };
}
