import { render, screen, fireEvent } from "@testing-library/react";
import { useGetAllActiveSessions } from "@/apps/new-ui/services/Service";
import ActiveAccounts from "@/apps/new-ui/pages/ActiveAccounts";

jest.mock("@/apps/new-ui/services/Service", () => ({
  useGetAllActiveSessions: jest.fn(),
}));

// Mock Accordion (مانع ارور UI شود)
jest.mock("@/components/ui/accordion", () => ({
  Accordion: ({ children }: any) => <div data-testid="accordion">{children}</div>,
  AccordionItem: ({ children }: any) => <div>{children}</div>,
  AccordionTrigger: ({ children, ...props }: any) => (
    <button data-testid="accordion-trigger" {...props}>
      {children}
    </button>
  ),
  AccordionContent: ({ children }: any) => (
    <div data-testid="accordion-content">{children}</div>
  ),
}));

describe("ActiveAccounts Component", () => {

  beforeEach(() => {
    jest.clearAllMocks();
  });

  test("shows loading state", () => {
    (useGetAllActiveSessions as jest.Mock).mockReturnValue({
      data: null,
      isPending: true,
    });

    render(<ActiveAccounts />);

    expect(
      screen.getByText("در حال بارگیری لیست دستگاه‌ها...")
    ).toBeInTheDocument();
  });

  test("shows empty state when sessions = []", () => {
    (useGetAllActiveSessions as jest.Mock).mockReturnValue({
      data: [],
      isPending: false,
    });

    render(<ActiveAccounts />);

    expect(screen.getByText("وضعیت خلوت")).toBeInTheDocument();

// نسخه درست:
expect(
  screen.getByText(/در حال حاضر هیچ نشست فعالی برای نمایش وجود ندارد/)
).toBeInTheDocument();

  });

  test("renders sessions when data exists", () => {
    (useGetAllActiveSessions as jest.Mock).mockReturnValue({
      data: [
        {
          id: 1,
          user_id: 10,
          device_id: "Mozilla/5.0 Chrome/119",
          ip_address: "192.168.1.10",
          last_activity: "1404-12-11T17:53:58",
          login_time: "1404-12-11T15:00:00",
        },
      ],
      isPending: false,
    });

    render(<ActiveAccounts />);

    expect(screen.getByTestId("accordion")).toBeInTheDocument();

    // مدل دستگاه در خروجی ظاهر شود
    expect(screen.getByText(/Chrome/)).toBeInTheDocument();

    // آی‌پی
    expect(screen.getByText("192.168.1.10")).toBeInTheDocument();

    // تاریخ فرمت شده
    expect(
      screen.getByText("1404/12/11 ساعت 17:53:58")
    ).toBeInTheDocument();
  });

  test("accordion opens and shows content", () => {
    (useGetAllActiveSessions as jest.Mock).mockReturnValue({
      data: [
        {
          id: 20,
          user_id: 33,
          device_id: "Mozilla/5.0 Firefox/121",
          ip_address: "8.8.8.8",
          last_activity: "1404-10-01T09:20:40",
          login_time: "1404-10-01T08:00:00",
        },
      ],
      isPending: false,
    });

    render(<ActiveAccounts />);

    const trigger = screen.getByTestId("accordion-trigger");
    expect(trigger).toBeInTheDocument();

    fireEvent.click(trigger);

    expect(screen.getByTestId("accordion-content")).toBeInTheDocument();
    expect(screen.getByText("8.8.8.8")).toBeInTheDocument();
  });
});
