import Factor from "@/apps/new-ui/pages/ReportAll/factor";
import { render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import * as FactorService from "@/apps/new-ui/services/Factor";
jest.mock("@/apps/new-ui/services/User", () => ({
  useGetUserInfo: () => ({
    data: {
      current_user: {
        id: 1,
        name: "test user",
        phone_number: "09120000000",
        profile_picture: "",
        user_id: "XX1",
        address: "",
        city: "",
        province: "",
        latitude: 0,
        longitude: 0,
        wallet_balance: 0,
        sms_balance: 0,
        services: [],
        score: 0,
        specialty: "",
        date_of_birth: "",
      },
      feeling_status: "",
      profile_complete: {
        active_time: null,
        has_profile_action: false
      },
      user_abilities_list: [
        {}, {}, {},
        {
          ability_action: "accountant_service",
          active: true
        }
      ]
    },
    isLoading: false
  })
}));


/* ===========================
   mocks: router
=========================== */
jest.mock("react-router-dom", () => ({
  ...jest.requireActual("react-router-dom"),
  useNavigate: () => jest.fn(),
  useLocation: () => ({
    pathname: "/factor",
    search: "",
  }),
}));

/* ===========================
   mocks: services
=========================== */
jest.mock("@/apps/new-ui/services/Factor", () => ({
  useGetCombinationAccountingData: jest.fn(),
  useDownloadTransactionsExcel: () => ({
    mutate: jest.fn(),
  }),
  useGetFinancialSummary: () => ({
    data: null,
    isLoading: false,
  }),
}));



jest.mock("@/apps/new-ui/services/IncomeAndCost", () => ({
  useDeleteIncome: () => ({
    mutate: jest.fn(),
  }),
  useDeleteExpense: () => ({
    mutate: jest.fn(),
  }),
}));

/* ===========================
   mocks: components
=========================== */
jest.mock("@/apps/new-ui/pages/ReportAll/+components/Header.tsx", () => () => (
  <div data-testid="header" />
));

jest.mock(
  "@/apps/new-ui/pages/ReportAll/factor/+components/Loader.tsx",
  () =>
    ({ text }: any) => <div data-testid="loader">{text}</div>,
);

jest.mock(
  "@/apps/new-ui/pages/ReportAll/+components/EmptyState.tsx",
  () =>
    ({ text }: any) => <div data-testid="empty">{text}</div>,
);

jest.mock(
  "@/apps/new-ui/pages/ReportAll/factor/+components/IncomeSwipper.tsx",
  () => () => <div data-testid="income-swipper" />,
);

jest.mock(
  "@/apps/new-ui/components/SubscriptionWrapper",
  () =>
    ({ children }: any) => <div>{children}</div>,
);

jest.mock(
  "@/apps/new-ui/pages/ReportAll/+components/CardList.tsx",
  () =>
    ({ items }: any) => (
      <div data-testid="card-list">
        {items.map((it: any) => (
          <div key={it.id}>{it.description}</div>
        ))}
      </div>
    ),
);




jest.mock(
  "@/apps/new-ui/components/CustomeComponents/ButtonActionsExcelIcon.tsx",
  () =>
    ({ onCreate }: any) => (
      <button data-testid="excel-btn" onClick={onCreate}>
        download
      </button>
    ),
);

/* ===========================
   helpers
=========================== */
const mockUseGetCombinationAccountingData =
  FactorService.useGetCombinationAccountingData as jest.Mock;

/* ===========================
   tests
=========================== */
describe("Factor page", () => {
  afterEach(() => {
    jest.clearAllMocks();
  });

  it("renders loader when loading", () => {
    mockUseGetCombinationAccountingData.mockReturnValue({
  data: {
    transactions: [
      { id: 1, type: "income", description: "درآمد تست", amount: 100 },
      { id: 2, type: "expense", description: "هزینه تست", amount: 50 }
    ]
  },
  isLoading: false,
  isError: false,
  refetch: jest.fn(),
});

    render(
      <MemoryRouter>
        <Factor />
      </MemoryRouter>,
    );

    expect(screen.getByTestId("loader")).toBeInTheDocument();
  });

  it("renders empty state when no transactions exist", () => {
    mockUseGetCombinationAccountingData.mockReturnValue({
      data: { transactions: [] },
      isLoading: false,
      isError: false,
      refetch: jest.fn(),
    });

    render(
      <MemoryRouter>
        <Factor />
      </MemoryRouter>,
    );

    expect(
      screen.getByText("هنوز درآمد یا هزینه‌ای ثبت نشده است."),
    ).toBeInTheDocument();
  });

it("renders card list when transactions exist", async () => {
  mockUseGetCombinationAccountingData.mockReturnValue({
    data: {
      transactions: [
        {
          id: 1,
          description: "درآمد تست",
          type: "income",
          amount: 1000,
          date: "1405-01-15",
        },
        {
          id: 2,
          description: "هزینه تست",
          type: "expense",
          amount: 500,
          date: "1405-01-10",
        },
      ],
    },
    isLoading: false,
    isError: false,
    refetch: jest.fn(),
  });

  render(
    <MemoryRouter>
      <Factor />
    </MemoryRouter>
  );

  expect(await screen.findByText("درآمد تست")).toBeInTheDocument();
  expect(await screen.findByText("هزینه تست")).toBeInTheDocument();
});


});
