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

vi.mock("@/features/payment/hooks/useRechargeProductsPage.js", () => ({
    useRechargeProductsPage: vi.fn(),
}));

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

test("recharge product page defaults to google product list", () => {
    vi.mocked(useRechargeProductsPage).mockReturnValue(pageFixture());

    render(<RechargeProductConfigPage />);

    expect(screen.getByRole("tab", { name: "Google", selected: true })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "新增 Google 商品" })).toBeInTheDocument();
    expect(screen.getAllByText("coins_480000").length).toBeGreaterThan(0);
    expect(screen.queryByText("支付汇率全球同步")).not.toBeInTheDocument();
});

test("third-party tab reuses product dialog and locks create platform to web", () => {
    const page = pageFixture();
    vi.mocked(useRechargeProductsPage).mockReturnValue(page);

    render(<RechargeProductConfigPage />);

    fireEvent.click(screen.getByRole("tab", { name: "三方" }));

    expect(screen.getByRole("tab", { name: "三方", selected: true })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "新增三方商品" })).toBeInTheDocument();
    expect(screen.queryByText("支付汇率全球同步")).not.toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "新增三方商品" }));

    expect(page.openCreate).toHaveBeenCalledTimes(1);
    expect(page.setForm).toHaveBeenCalledWith({ platform: "web" });
});

function pageFixture() {
    return {
        abilities: {
            canCreateProduct: true,
            canDeleteProduct: true,
            canUpdateProduct: true,
        },
        activeAction: "",
        audienceType: "",
        closeDialog: vi.fn(),
        data: {
            items: [
                {
                    amountUsdt: "5.99",
                    audienceType: "normal",
                    coinAmount: 480000,
                    description: "480,000 coins",
                    enabled: true,
                    platform: "android",
                    productCode: "coins_480000",
                    productId: 1,
                    productName: "coins_480000",
                    regionIds: [1, 2],
                    updatedAtMs: 1760000000000,
                },
            ],
            total: 1,
        },
        editingItem: null,
        error: null,
        form: {
            amountUsdt: "",
            audienceType: "normal",
            coinAmount: "",
            description: "",
            enabled: true,
            platform: "android",
            productName: "",
            regionIds: [],
        },
        keyword: "",
        loading: false,
        loadingAction: "",
        loadingRegions: false,
        openCreate: vi.fn(),
        openEdit: vi.fn(),
        page: 1,
        pageSize: 50,
        platform: "android",
        regionId: "",
        regionOptions: [
            { label: "中东 · ME · 1", name: "中东", value: "1" },
            { label: "欧洲美区 · EU · 2", name: "欧洲美区", value: "2" },
        ],
        reload: vi.fn(),
        removeProduct: vi.fn(),
        resetFilters: vi.fn(),
        setAudienceType: vi.fn(),
        setForm: vi.fn(),
        setKeyword: vi.fn(),
        setPage: vi.fn(),
        setPlatform: vi.fn(),
        setRegionId: vi.fn(),
        setStatus: vi.fn(),
        status: "",
        submitProduct: vi.fn(),
        toggleProductEnabled: vi.fn(),
    };
}
