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

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

test("loads the selected app USDT address and copies the exact SQL value", async () => {
    const writeText = vi.fn(async () => undefined);
    vi.stubGlobal("navigator", { clipboard: { writeText } });
    const onLoad = vi.fn(async (appCode) => ({
        appCode,
        items: [
            {
                address: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
                chain: "TRON",
                updatedAtMs: 1760000000000,
            },
        ],
    }));

    render(
        <FinanceUSDTAddressDialog
            apps={[
                { appCode: "lalu", appName: "Lalu" },
                { appCode: "aslan", appName: "Aslan" },
            ]}
            initialAppCode="aslan"
            open
            onClose={vi.fn()}
            onLoad={onLoad}
        />,
    );

    expect(await screen.findByText("TRON · TRC20")).toBeInTheDocument();
    expect(screen.getByText("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t")).toBeInTheDocument();
    expect(onLoad).toHaveBeenCalledWith("aslan");

    fireEvent.click(screen.getByRole("button", { name: "复制 TRON 地址" }));
    await waitFor(() => expect(writeText).toHaveBeenCalledWith("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"));
    expect(screen.getByRole("button", { name: "复制 TRON 地址" })).toHaveTextContent("已复制");
});

test("modifies an existing TRON address and keeps the saved SQL value in the row", async () => {
    const originalAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
    const updatedAddress = "TLv7wERsM42ZeobMrrKVC5C72d3gFUa44J";
    const onSave = vi.fn(async (appCode, chain, payload) => ({
        address: payload.address,
        appCode,
        chain,
        updatedAtMs: 200,
    }));

    render(
        <FinanceUSDTAddressDialog
            apps={[{ appCode: "aslan", appName: "Aslan" }]}
            canUpdate
            initialAppCode="aslan"
            open
            onClose={vi.fn()}
            onLoad={vi.fn(async () => ({ appCode: "aslan", items: [{ address: originalAddress, chain: "TRON" }] }))}
            onSave={onSave}
        />,
    );

    await screen.findByText(originalAddress);
    fireEvent.click(screen.getByRole("button", { name: "修改 TRON 地址" }));
    fireEvent.change(screen.getByRole("textbox", { name: "TRON · TRC20 收款地址" }), { target: { value: updatedAddress } });
    fireEvent.click(screen.getByRole("button", { name: "保存 TRON 地址" }));

    await waitFor(() => expect(onSave).toHaveBeenCalledWith("aslan", "TRON", { address: updatedAddress }));
    expect(await screen.findByText(updatedAddress)).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "修改 TRON 地址" })).toBeInTheDocument();
});

test("shows an add action when the selected app has no TRON address", async () => {
    render(
        <FinanceUSDTAddressDialog
            apps={[{ appCode: "aslan", appName: "Aslan" }]}
            canUpdate
            initialAppCode="aslan"
            open
            onClose={vi.fn()}
            onLoad={vi.fn(async () => ({ appCode: "aslan", items: [] }))}
            onSave={vi.fn()}
        />,
    );

    expect(await screen.findByRole("button", { name: "添加 TRON 地址" })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "添加 BSC 地址" })).toBeInTheDocument();
});
