import { fireEvent, render, screen } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { ToastProvider } from "@/shared/ui/ToastProvider.jsx";
import { FinanceWithdrawalApplicationList } from "./FinanceWithdrawalApplicationList.jsx";

const baseProps = {
  applications: {
    items: [],
    page: 1,
    pageSize: 50,
    total: 0
  },
  error: "",
  filters: {
    appCode: "",
    keyword: "",
    page: 1
  },
  loading: false,
  onAudit: vi.fn(),
  onFiltersChange: vi.fn(),
  onReload: vi.fn(),
  options: { apps: [] }
};

function renderList(props) {
  return render(
    <ToastProvider>
      <FinanceWithdrawalApplicationList {...props} />
    </ToastProvider>
  );
}

test("renders withdrawal application table with requested finance columns", () => {
  renderList({
    ...baseProps,
    applications: {
      ...baseProps.applications,
      items: [
        {
          appCode: "lalu",
          approvedAtMs: 1760007200000,
          approverName: "财务一",
          approverUserId: 21,
          auditRemark: "地址不符合要求",
          createdAtMs: 1760000000000,
          id: 7,
          operationsAuditRemark: "资料无误",
          operationsReviewedAtMs: 1760003600000,
          operationsReviewerName: "运营一",
          operationsReviewerUserId: 11,
          operationsStatus: "approved",
          sourceApplicationId: "991001",
          sourceSystem: "likei",
          status: "approved",
          userId: "10001",
          withdrawAddress: "TRX-address",
          withdrawAmount: 12.5,
          withdrawMethod: "USDT-TRC20"
        }
      ],
      total: 1
    }
  });

  expect(screen.getByRole("columnheader", { name: "APP" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "来源单号" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "用户ID" })).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.getByRole("columnheader", { name: "财务审核时间" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "财务审核原因" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "操作" })).toBeInTheDocument();
  expect(screen.getByText("lalu")).toBeInTheDocument();
  expect(screen.getByText("Likei")).toBeInTheDocument();
  expect(screen.getByText("991001")).toBeInTheDocument();
  expect(screen.getByText("10001")).toBeInTheDocument();
  expect(screen.getByText("$ 12.5")).toBeInTheDocument();
  expect(screen.getByText("USDT-TRC20")).toBeInTheDocument();
  expect(screen.getByText("TRX-address")).toBeInTheDocument();
  expect(screen.getByText("财务一")).toBeInTheDocument();
  expect(screen.getByText("21")).toBeInTheDocument();
  expect(screen.getByText("运营一")).toBeInTheDocument();
  expect(screen.getByText("11")).toBeInTheDocument();
  expect(screen.getByText("资料无误")).toBeInTheDocument();
  expect(screen.getByText("地址不符合要求")).toBeInTheDocument();
});

test("keeps empty withdrawal table colspan aligned with visible columns", () => {
  const { container } = renderList(baseProps);

  expect(container.querySelector("tbody td")?.colSpan).toBe(12);
  expect(screen.getByText("当前无数据")).toBeInTheDocument();
});

test("operations review only exposes actions for operations pending applications", () => {
  const onAudit = vi.fn();
  renderList({
    ...baseProps,
    canAudit: true,
    onAudit,
    reviewStage: "operations",
    applications: {
      ...baseProps.applications,
      items: [
        {
          appCode: "lalu",
          createdAtMs: 1760000000000,
          id: 8,
          operationsStatus: "pending",
          status: "pending",
          userId: "10002",
          withdrawAddress: "TRX-operations",
          withdrawAmount: 20,
          withdrawMethod: "USDT-TRC20"
        }
      ],
      total: 1
    }
  });

  expect(screen.getByRole("columnheader", { name: "运营审核人" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "运营审核时间" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "运营审核原因" })).toBeInTheDocument();
  fireEvent.click(screen.getByRole("button", { name: "通过" }));
  expect(onAudit).toHaveBeenCalledWith(expect.objectContaining({ id: 8, decision: "approved", reviewStage: "operations" }));
});

test("operations rejecting applications expose only rejection retry", () => {
  const onAudit = vi.fn();
  renderList({
    ...baseProps,
    canAudit: true,
    onAudit,
    reviewStage: "operations",
    applications: {
      ...baseProps.applications,
      items: [
        {
          appCode: "lalu",
          createdAtMs: 1760000000000,
          id: 10,
          operationsAuditRemark: "命中运营风控",
          operationsStatus: "rejecting",
          status: "pending",
          userId: "10004",
          withdrawAddress: "TRX-rejecting",
          withdrawAmount: 40,
          withdrawMethod: "USDT-TRC20"
        }
      ],
      total: 1
    }
  });

  expect(screen.getByText("拒绝处理中")).toBeInTheDocument();
  expect(screen.queryByRole("button", { name: "通过" })).not.toBeInTheDocument();
  expect(screen.queryByRole("button", { name: "拒绝" })).not.toBeInTheDocument();
  fireEvent.click(screen.getByRole("button", { name: "重试拒绝" }));
  expect(onAudit).toHaveBeenCalledWith(
    expect.objectContaining({
      decision: "rejected",
      id: 10,
      operationsAuditRemark: "命中运营风控",
      reviewStage: "operations"
    })
  );
});

test("finance review actions follow the overall pending status", () => {
  const onAudit = vi.fn();
  renderList({
    ...baseProps,
    canAudit: true,
    onAudit,
    reviewStage: "finance",
    applications: {
      ...baseProps.applications,
      items: [
        {
          appCode: "lalu",
          createdAtMs: 1760000000000,
          id: 9,
          operationsStatus: "approved",
          status: "pending",
          userId: "10003",
          withdrawAddress: "TRX-finance",
          withdrawAmount: 30,
          withdrawMethod: "USDT-TRC20"
        }
      ],
      total: 1
    }
  });

  fireEvent.click(screen.getByRole("button", { name: "拒绝" }));
  expect(onAudit).toHaveBeenCalledWith(expect.objectContaining({ id: 9, decision: "rejected", reviewStage: "finance" }));
});
