package localization

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
)

type mockLocalizationRepository struct {
	mock.Mock
}

func (m *mockLocalizationRepository) GetTranslation(ctx context.Context, key string, language string) (string, error) {
	args := m.Called(ctx, key, language)
	return args.String(0), args.Error(1)
}

func (m *mockLocalizationRepository) GetTranslations(ctx context.Context, keys []string, language string) (map[string]string, error) {
	args := m.Called(ctx, keys, language)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(map[string]string), args.Error(1)
}

func (m *mockLocalizationRepository) GetAuthorBiography(ctx context.Context, authorID uint, language string) (string, error) {
	args := m.Called(ctx, authorID, language)
	return args.String(0), args.Error(1)
}

func (m *mockLocalizationRepository) GetWorkContent(ctx context.Context, workID uint, language string) (string, error) {
	args := m.Called(ctx, workID, language)
	return args.String(0), args.Error(1)
}

func TestLocalizationService_GetTranslation(t *testing.T) {
	repo := new(mockLocalizationRepository)
	service := NewService(repo)

	ctx := context.Background()
	key := "test_key"
	language := "en"
	expectedTranslation := "Test Translation"

	repo.On("GetTranslation", ctx, key, language).Return(expectedTranslation, nil)

	translation, err := service.Queries.GetTranslation(ctx, key, language)

	assert.NoError(t, err)
	assert.Equal(t, expectedTranslation, translation)
	repo.AssertExpectations(t)
}

func TestLocalizationService_GetTranslations(t *testing.T) {
	repo := new(mockLocalizationRepository)
	service := NewService(repo)

	ctx := context.Background()
	keys := []string{"key1", "key2"}
	language := "en"
	expectedTranslations := map[string]string{
		"key1": "Translation 1",
		"key2": "Translation 2",
	}

	repo.On("GetTranslations", ctx, keys, language).Return(expectedTranslations, nil)

	translations, err := service.Queries.GetTranslations(ctx, keys, language)

	assert.NoError(t, err)
	assert.Equal(t, expectedTranslations, translations)
	repo.AssertExpectations(t)
}

func TestLocalizationService_GetAuthorBiography(t *testing.T) {
	repo := new(mockLocalizationRepository)
	service := NewService(repo)

	ctx := context.Background()
	authorID := uint(1)
	language := "en"
	expectedBiography := "This is a test biography."

	repo.On("GetAuthorBiography", ctx, authorID, language).Return(expectedBiography, nil)

	biography, err := service.Queries.GetAuthorBiography(ctx, authorID, language)

	assert.NoError(t, err)
	assert.Equal(t, expectedBiography, biography)
	repo.AssertExpectations(t)
}