package translation

import (
	"context"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
	"tercul/internal/domain"
	"testing"
)

type TranslationQueriesSuite struct {
	suite.Suite
	repo    *mockTranslationRepository
	queries *TranslationQueries
}

func (s *TranslationQueriesSuite) SetupTest() {
	s.repo = &mockTranslationRepository{}
	s.queries = NewTranslationQueries(s.repo)
}

func TestTranslationQueriesSuite(t *testing.T) {
	suite.Run(t, new(TranslationQueriesSuite))
}

func (s *TranslationQueriesSuite) TestTranslation_Success() {
	translation := &domain.Translation{
		BaseModel:      domain.BaseModel{ID: 1},
		Title:          "Test Translation",
		Language:       "es",
		Content:        "Test content",
		TranslatableID: 1,
	}
	s.repo.getByIDFunc = func(ctx context.Context, id uint) (*domain.Translation, error) {
		return translation, nil
	}
	dto, err := s.queries.Translation(context.Background(), 1)
	assert.NoError(s.T(), err)

	expectedDTO := &TranslationDTO{
		ID:             1,
		Title:          "Test Translation",
		Language:       "es",
		Content:        "Test content",
		TranslatableID: 1,
	}
	assert.Equal(s.T(), expectedDTO, dto)
}

func (s *TranslationQueriesSuite) TestListTranslations_Success() {
	domainTranslations := &domain.PaginatedResult[domain.Translation]{
		Items: []domain.Translation{
			{BaseModel: domain.BaseModel{ID: 1}, Title: "Translation 1", Language: "es", Content: "Content 1", TranslatableID: 1},
			{BaseModel: domain.BaseModel{ID: 2}, Title: "Translation 2", Language: "fr", Content: "Content 2", TranslatableID: 1},
		},
		TotalCount: 2,
		Page:       1,
		PageSize:   10,
		TotalPages: 1,
	}
	s.repo.listByWorkIDPaginatedFunc = func(ctx context.Context, workID uint, language *string, page, pageSize int) (*domain.PaginatedResult[domain.Translation], error) {
		return domainTranslations, nil
	}

	paginatedDTOs, err := s.queries.ListTranslations(context.Background(), 1, nil, 1, 10)
	assert.NoError(s.T(), err)

	expectedDTOs := &domain.PaginatedResult[TranslationDTO]{
		Items: []TranslationDTO{
			{ID: 1, Title: "Translation 1", Language: "es", Content: "Content 1", TranslatableID: 1},
			{ID: 2, Title: "Translation 2", Language: "fr", Content: "Content 2", TranslatableID: 1},
		},
		TotalCount: 2,
		Page:       1,
		PageSize:   10,
		TotalPages: 1,
	}
	assert.Equal(s.T(), expectedDTOs, paginatedDTOs)
}