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

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

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

test("third-party payment sync button opens rate markup dialog", () => {
    const page = pageFixture({ openSyncRateDialog: vi.fn() });
    vi.mocked(useThirdPartyPaymentPage).mockReturnValue(page);

    renderPage();

    fireEvent.click(screen.getByRole("button", { name: "支付汇率全球同步" }));

    expect(page.openSyncRateDialog).toHaveBeenCalledTimes(1);
});

test("third-party payment rate dialog submits markup percent", () => {
    const page = pageFixture({
        setSyncRateMarkupPercent: vi.fn(),
        submitSyncRates: vi.fn((event) => event.preventDefault()),
        syncRateDialogOpen: true,
    });
    vi.mocked(useThirdPartyPaymentPage).mockReturnValue(page);

    renderPage();

    fireEvent.change(screen.getByRole("textbox", { name: /汇率上浮比例/ }), { target: { value: "3" } });
    fireEvent.click(screen.getByRole("button", { name: "确定" }));

    expect(page.setSyncRateMarkupPercent).toHaveBeenCalledWith("3");
    expect(page.submitSyncRates).toHaveBeenCalledTimes(1);
});

test("third-party payment page shows v5pay method sync button instead of status text", () => {
    const v5pay = { providerCode: "v5pay", providerName: "V5Pay", status: "active", methods: [] };
    const page = pageFixture({
        activeChannel: v5pay,
        channels: [{ providerCode: "mifapay", providerName: "MiFaPay", status: "active", methods: [] }, v5pay],
        syncPaymentMethods: vi.fn(),
    });
    vi.mocked(useThirdPartyPaymentPage).mockReturnValue(page);

    renderPage();

    expect(screen.queryByText("已启用")).not.toBeInTheDocument();
    fireEvent.click(screen.getByRole("button", { name: "同步支付方式" }));

    expect(page.syncPaymentMethods).toHaveBeenCalledWith(v5pay);
});

function renderPage() {
    return render(
        <MemoryRouter>
            <ThirdPartyPaymentPage />
        </MemoryRouter>,
    );
}

function pageFixture(overrides = {}) {
    return {
        abilities: {
            canUpdateThirdParty: true,
        },
        activeChannel: null,
        channels: [],
        closeSyncRateDialog: vi.fn(),
        error: null,
        expandedChannel: "",
        loading: false,
        loadingAction: "",
        openSyncRateDialog: vi.fn(),
        rateDrafts: {},
        reload: vi.fn(),
        saveRate: vi.fn(),
        setRateDraft: vi.fn(),
        setSyncRateMarkupPercent: vi.fn(),
        syncPaymentMethods: vi.fn(),
        submitSyncRates: vi.fn((event) => event.preventDefault()),
        syncRateDialogOpen: false,
        syncRateForm: { markupPercent: "0" },
        toggleChannel: vi.fn(),
        toggleMethod: vi.fn(),
        ...overrides,
    };
}
