package repository

import (
	"testing"

	"github.com/stretchr/testify/require"
	"github.com/user-management-system/internal/pkg/pagination"
)

func TestPaginationResultFromTotal(t *testing.T) {
	tests := []struct {
		name       string
		total      int64
		page       int
		pageSize   int
		wantPages  int
		wantTotal  int64
		wantPage   int
		wantLimit  int
	}{
		{
			name:      "exact_division",
			total:     100,
			page:      1,
			pageSize:  20,
			wantPages: 5,
			wantTotal: 100,
			wantPage:  1,
			wantLimit: 20,
		},
		{
			name:      "with_remainder",
			total:     105,
			page:      1,
			pageSize:  20,
			wantPages: 6,
			wantTotal: 105,
			wantPage:  1,
			wantLimit: 20,
		},
		{
			name:      "zero_total",
			total:     0,
			page:      1,
			pageSize:  20,
			wantPages: 0,
			wantTotal: 0,
			wantPage:  1,
			wantLimit: 20,
		},
		{
			name:      "small_page_size",
			total:     10,
			page:      1,
			pageSize:  5,
			wantPages: 2,
			wantTotal: 10,
			wantPage:  1,
			wantLimit: 5,
		},
		{
			name:      "page_size_over_100",
			total:     100,
			page:      1,
			pageSize:  150,
			wantPages: 1,
			wantTotal: 100,
			wantPage:  1,
			// Limit() caps at 100
			wantLimit: 100,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			params := pagination.PaginationParams{Page: tt.page, PageSize: tt.pageSize}
			result := paginationResultFromTotal(tt.total, params)

			require.Equal(t, tt.wantTotal, result.Total)
			require.Equal(t, tt.wantPage, result.Page)
			require.Equal(t, tt.wantLimit, result.PageSize)
			require.Equal(t, tt.wantPages, result.Pages)
		})
	}
}
