import {
  INITIAL_PAGE_INDEX,
  INITIAL_PAGE_SIZE,
  MAX_PAGE_SIZE,
  MIN_PAGE_SIZE,
} from "@repo/rdx-criteria";

import { act, renderHook } from "@testing-library/react-hooks";

import { PaginationState, usePagination } from "./use-pagination";

describe("usePagination", () => {
  it("should initialize with default values", () => {
    const { result } = renderHook(() => usePagination());
    const [pagination] = result.current;

    expect(pagination.pageIndex).toBe(INITIAL_PAGE_INDEX);
    expect(pagination.pageSize).toBe(INITIAL_PAGE_SIZE);
  });

  it("should initialize with custom values", () => {
    const customPageIndex = 2;
    const customPageSize = 20;
    const { result } = renderHook(() => usePagination(customPageIndex, customPageSize));
    const [pagination] = result.current;

    expect(pagination.pageIndex).toBe(customPageIndex);
    expect(pagination.pageSize).toBe(customPageSize);
  });

  it("should update pagination state correctly", () => {
    const { result } = renderHook(() => usePagination());
    const [pagination, updatePagination] = result.current;

    const newPagination: PaginationState = { pageIndex: 1, pageSize: 25 };

    act(() => {
      updatePagination(newPagination);
    });

    expect(pagination.pageIndex).toBe(newPagination.pageIndex);
    expect(pagination.pageSize).toBe(newPagination.pageSize);
  });

  it("should not update pageIndex below INITIAL_PAGE_INDEX", () => {
    const { result } = renderHook(() => usePagination());
    const [, updatePagination] = result.current;

    act(() => {
      updatePagination({ pageIndex: -1, pageSize: INITIAL_PAGE_SIZE });
    });

    const [pagination] = result.current;

    expect(pagination.pageIndex).toBe(INITIAL_PAGE_INDEX);
  });

  it("should not update pageSize below MIN_PAGE_SIZE or above MAX_PAGE_SIZE", () => {
    const { result } = renderHook(() => usePagination());
    const [, updatePagination] = result.current;

    act(() => {
      updatePagination({ pageIndex: INITIAL_PAGE_INDEX, pageSize: MIN_PAGE_SIZE - 1 });
    });

    let [pagination] = result.current;
    expect(pagination.pageSize).toBe(INITIAL_PAGE_SIZE); // No cambia porque pageSize es menor que el mínimo

    act(() => {
      updatePagination({ pageIndex: INITIAL_PAGE_INDEX, pageSize: MAX_PAGE_SIZE + 1 });
    });

    [pagination] = result.current;
    expect(pagination.pageSize).toBe(INITIAL_PAGE_SIZE); // No cambia porque pageSize es mayor que el máximo
  });
});
