import { render, screen } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { FinanceRechargeDetailList } from "./FinanceRechargeDetailList.jsx";

const baseProps = {
    apps: [
        { appCode: "lalu", appName: "lalu" },
        { appCode: "aslan", appName: "Aslan" },
    ],
    bills: {
        items: [],
        page: 1,
        pageSize: 50,
        total: 0,
    },
    error: "",
    filters: {
        appCode: "",
        keyword: "",
        page: 1,
        regionId: "",
        timeRange: { endMs: "", startMs: "" },
    },
    loading: false,
    onFiltersChange: vi.fn(),
    onReload: vi.fn(),
    summary: {
        googlePlay: { billCount: 1, coinAmount: 90000, usdMinorAmount: 999 },
        thirdParty: { billCount: 0, coinAmount: 0, usdMinorAmount: 0 },
        total: { billCount: 1, coinAmount: 90000, usdMinorAmount: 999 },
    },
    summaryLoading: false,
};

test("hides coin recharge columns by default", () => {
    render(
        <FinanceRechargeDetailList
            {...baseProps}
            bills={{
                ...baseProps.bills,
                items: [rechargeBillFixture()],
                total: 1,
            }}
        />,
    );

    expect(screen.queryByRole("columnheader", { name: "充值金币" })).not.toBeInTheDocument();
    expect(screen.queryByText("90,000 金币")).not.toBeInTheDocument();
    expect(screen.queryByText("1 笔 · 90,000 金币")).not.toBeInTheDocument();
    expect(screen.getByRole("columnheader", { name: "账单金额" })).toBeInTheDocument();
    expect(screen.getByRole("columnheader", { name: "用户实付" })).toBeInTheDocument();
});

test("renders app recharge coin columns and values when coin currency is selected", () => {
    render(
        <FinanceRechargeDetailList
            {...baseProps}
            bills={{
                ...baseProps.bills,
                items: [rechargeBillFixture()],
                total: 1,
            }}
            currency="coin"
        />,
    );

    expect(screen.getByRole("columnheader", { name: "APP" })).toBeInTheDocument();
    expect(screen.getByRole("columnheader", { name: "充值金币" })).toBeInTheDocument();
    expect(screen.getByRole("columnheader", { name: "充值来源" })).toBeInTheDocument();
    expect(screen.getByRole("columnheader", { name: "充值订单号" })).toBeInTheDocument();
    expect(screen.getByRole("columnheader", { name: "账单金额" })).toBeInTheDocument();
    expect(screen.getByRole("columnheader", { name: "用户实付" })).toBeInTheDocument();
    expect(screen.getByRole("columnheader", { name: "三方税率扣款" })).toBeInTheDocument();
    expect(screen.getByText("Aslan")).toBeInTheDocument();
    expect(screen.getByText("90,000 金币")).toBeInTheDocument();
    expect(screen.getAllByText("谷歌充值").length).toBeGreaterThanOrEqual(1);
    expect(screen.getByText("tx-google")).toBeInTheDocument();
    expect(screen.getByText("cmd-1 / GPA.1")).toBeInTheDocument();
    expect(screen.getByText("USDT 9.99")).toBeInTheDocument();
    expect(screen.getByText("SAR 12.99")).toBeInTheDocument();
    expect(screen.getByText("SAR 1.95 / 15%")).toBeInTheDocument();
});

test("keeps bill amount in USDT while rendering third-party paid amount in local currency", () => {
    render(
        <FinanceRechargeDetailList
            {...baseProps}
            bills={{
                ...baseProps.bills,
                items: [
                    {
                        ...rechargeBillFixture(),
                        currencyCode: "EGP",
                        id: "tx-mifapay",
                        providerAmountMinor: 472600,
                        rechargeType: "third_party_recharge",
                        transactionId: "tx-mifapay",
                        usdMinorAmount: 10000,
                        userPaidAmountMicro: 4726000000,
                        userPaidCurrencyCode: "EGP",
                        userPaidText: "",
                    },
                ],
                total: 1,
            }}
        />,
    );

    expect(screen.getByText("USDT 100.00")).toBeInTheDocument();
    expect(screen.getByText("EGP 4,726.00")).toBeInTheDocument();
    expect(screen.queryByText("EGP 100.00")).not.toBeInTheDocument();
});

test("keeps empty recharge detail table colspan aligned with visible columns", () => {
    const { container } = render(<FinanceRechargeDetailList {...baseProps} />);

    expect(container.querySelector("tbody td")?.colSpan).toBe(8);
    expect(screen.getByText("当前筛选条件下没有账单")).toBeInTheDocument();

    const { container: coinContainer } = render(<FinanceRechargeDetailList {...baseProps} currency="coin" />);
    expect(coinContainer.querySelector("tbody td")?.colSpan).toBe(9);
});

function rechargeBillFixture() {
    return {
        appCode: "aslan",
        coinAmount: 90000,
        commandId: "cmd-1",
        createdAtMs: 1760000000000,
        currencyCode: "SAR",
        exchangeCoinAmount: 90000,
        exchangeUsdMinorAmount: 999,
        externalRef: "GPA.1",
        id: "tx-google",
        paidSyncedAtMs: 1760000100000,
        providerAmountMinor: 1299,
        providerTaxMicro: 1950000,
        rechargeType: "google_play_recharge",
        transactionId: "tx-google",
        usdMinorAmount: 999,
        userPaidAmountMicro: 13000000,
        userPaidCurrencyCode: "SAR",
        userPaidText: "SAR 12.99",
    };
}
