package repositories

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/treytartt/honeydue-api/internal/models"
	"github.com/treytartt/honeydue-api/internal/testutil"
)

func TestDocumentRepository_FindByUser_HasDefaultLimit(t *testing.T) {
	db := testutil.SetupTestDB(t)
	repo := NewDocumentRepository(db)

	user := testutil.CreateTestUser(t, db, "owner", "owner@test.com", "password")
	residence := testutil.CreateTestResidence(t, db, user.ID, "Test House")

	// Create 510 documents to exceed the default limit of 500
	for i := 0; i < 510; i++ {
		doc := &models.Document{
			ResidenceID:  residence.ID,
			CreatedByID:  user.ID,
			Title:        fmt.Sprintf("Doc %d", i+1),
			DocumentType: models.DocumentTypeGeneral,
			FileURL:      "https://example.com/doc.pdf",
			IsActive:     true,
		}
		err := db.Create(doc).Error
		require.NoError(t, err)
	}

	docs, err := repo.FindByUser([]uint{residence.ID})
	require.NoError(t, err)
	assert.Equal(t, 500, len(docs), "FindByUser should return at most 500 documents by default")
}
