package com.gnx.telemedicine.util;

import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Unit tests for PaginationUtil utility class.
 */
class PaginationUtilTest {

    @Test
    void testCreatePageable() {
        Pageable pageable = PaginationUtil.createPageable(0, 20);
        assertNotNull(pageable, "Should create Pageable");
        assertEquals(0, pageable.getPageNumber(), "Should have correct page number");
        assertEquals(20, pageable.getPageSize(), "Should have correct page size");
    }

    @Test
    void testCreatePageableWithSorting() {
        Pageable pageable = PaginationUtil.createPageable(0, 20, "email", "ASC");
        assertNotNull(pageable, "Should create Pageable with sorting");
        assertTrue(pageable.getSort().isSorted(), "Should have sorting");
        assertEquals(Sort.Direction.ASC, pageable.getSort().getOrderFor("email").getDirection());
    }

    @Test
    void testCreatePageableWithDefaultValues() {
        Pageable pageable = PaginationUtil.createPageable(null, null);
        assertEquals(0, pageable.getPageNumber(), "Should default to page 0");
        assertEquals(20, pageable.getPageSize(), "Should default to size 20");
    }

    @Test
    void testCreatePageableWithMaxSize() {
        Pageable pageable = PaginationUtil.createPageable(0, 200);
        assertEquals(100, pageable.getPageSize(), "Should cap at max size 100");
    }

    @Test
    void testCreatePageableWithMinSize() {
        Pageable pageable = PaginationUtil.createPageable(0, 0);
        assertEquals(1, pageable.getPageSize(), "Should enforce min size 1");
    }

    @Test
    void testValidatePage() {
        assertEquals(0, PaginationUtil.validatePage(0), "Should accept valid page");
        assertEquals(0, PaginationUtil.validatePage(null), "Should default to 0");
        assertEquals(0, PaginationUtil.validatePage(-1), "Should default negative to 0");
    }

    @Test
    void testValidateSize() {
        assertEquals(20, PaginationUtil.validateSize(20), "Should accept valid size");
        assertEquals(20, PaginationUtil.validateSize(null), "Should default to 20");
        assertEquals(1, PaginationUtil.validateSize(0), "Should enforce min size 1");
        assertEquals(100, PaginationUtil.validateSize(200), "Should cap at max size 100");
    }

    @Test
    void testCreateSort() {
        Sort sort = PaginationUtil.createSort("email", "ASC");
        assertNotNull(sort, "Should create Sort");
        assertEquals(Sort.Direction.ASC, sort.getOrderFor("email").getDirection());
    }

    @Test
    void testCreateSortDescending() {
        Sort sort = PaginationUtil.createSort("email", "DESC");
        assertEquals(Sort.Direction.DESC, sort.getOrderFor("email").getDirection());
    }

    @Test
    void testCreateSortWithNullField() {
        Sort sort = PaginationUtil.createSort(null, "ASC");
        assertFalse(sort.isSorted(), "Should return unsorted for null field");
    }
}

