import { fireEvent, render, screen, within } from "@testing-library/react";
import { afterEach, expect, test, vi } from "vitest";
import { PaymentBillListPage } from "@/features/payment/pages/PaymentBillListPage.jsx";
import { usePaginatedQuery } from "@/shared/hooks/usePaginatedQuery.js";
import { useRegionOptions } from "@/shared/hooks/useRegionOptions.js";

vi.mock("@/shared/hooks/usePaginatedQuery.js", () => ({
    usePaginatedQuery: vi.fn(),
}));

vi.mock("@/shared/hooks/useRegionOptions.js", () => ({
    useRegionOptions: vi.fn(),
}));

vi.mock("@/shared/ui/ToastProvider.jsx", () => ({
    useToast: () => ({ showToast: vi.fn() }),
}));

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

test("payment bills show user seller country region and split amount columns", () => {
    mockRegions();
    vi.mocked(usePaginatedQuery).mockReturnValue(queryFixture([coinSellerBill()]));

    render(<PaymentBillListPage />);

    expect(screen.getByText("用户信息")).toBeInTheDocument();
    expect(screen.getByText("币商信息")).toBeInTheDocument();
    expect(screen.getByText("金币")).toBeInTheDocument();
    expect(screen.getByText("金额")).toBeInTheDocument();
    expect(screen.getByText("国家/区域")).toBeInTheDocument();
    expect(screen.queryByText("兑换口径")).not.toBeInTheDocument();
    expect(screen.queryByText("金币 / 金额")).not.toBeInTheDocument();

    expect(screen.getByText("Alice")).toBeInTheDocument();
    expect(screen.getByText("165075")).toBeInTheDocument();
    expect(screen.getByText("Seller")).toBeInTheDocument();
    expect(screen.getByText("164917")).toBeInTheDocument();
    expect(screen.getByText("菲律宾")).toBeInTheDocument();
    expect(screen.getByText("东南亚")).toBeInTheDocument();
    expect(screen.queryByText("USD 0.00")).not.toBeInTheDocument();
});

test("payment bills include google recharge option and region header filter", () => {
    mockRegions();
    vi.mocked(usePaginatedQuery).mockReturnValue(queryFixture([googleBill()]));

    render(<PaymentBillListPage />);

    expect(screen.getByText("谷歌充值")).toBeInTheDocument();
    expect(screen.getByText("USDT 0.99")).toBeInTheDocument();

    fireEvent.click(screen.getAllByRole("button", { name: /国家\/区域/ })[0]);

    const filter = screen.getByRole("presentation");
    expect(within(filter).getByText("全部区域")).toBeInTheDocument();
    expect(within(filter).getByText("中东 · ME · 2")).toBeInTheDocument();
});

function mockRegions() {
    vi.mocked(useRegionOptions).mockReturnValue({
        loadingRegions: false,
        regionOptions: [
            { label: "东南亚 · SEA · 1", name: "东南亚", regionId: 1, value: "1" },
            { label: "中东 · ME · 2", name: "中东", regionId: 2, value: "2" },
        ],
    });
}

function queryFixture(items) {
    return {
        data: { items, page: 1, pageSize: 50, total: items.length },
        error: null,
        loading: false,
        reload: vi.fn(),
    };
}

function coinSellerBill() {
    return {
        coinAmount: 130000,
        createdAtMs: 1760000000000,
        currencyCode: "USD",
        rechargeType: "coin_seller_transfer",
        seller: { displayUserId: "164917", userId: "2002", username: "Seller" },
        sellerUserId: 2002,
        status: "succeeded",
        targetRegionId: 1,
        transactionId: "tx_coin",
        usdMinorAmount: 0,
        user: {
            countryCode: "PH",
            countryDisplayName: "菲律宾",
            displayUserId: "165075",
            userId: "1001",
            username: "Alice",
        },
        userId: 1001,
    };
}

function googleBill() {
    return {
        coinAmount: 80000,
        createdAtMs: 1760000000000,
        currencyCode: "USDT",
        rechargeType: "google_play_recharge",
        status: "succeeded",
        targetRegionId: 2,
        transactionId: "tx_google",
        usdMinorAmount: 99,
        user: {
            countryCode: "AE",
            countryDisplayName: "阿联酋",
            displayUserId: "165108",
            userId: "1002",
            username: "Google User",
        },
        userId: 1002,
    };
}
