package testutil

import (
	"context"
	"gorm.io/gorm"
	"tercul/internal/domain"
)

import "github.com/stretchr/testify/mock"

// MockTranslationRepository is an in-memory implementation of TranslationRepository
type MockTranslationRepository struct {
	mock.Mock
}

func NewMockTranslationRepository() *MockTranslationRepository {
	return new(MockTranslationRepository)
}

var _ domain.TranslationRepository = (*MockTranslationRepository)(nil)

// BaseRepository methods with context support
func (m *MockTranslationRepository) Create(ctx context.Context, t *domain.Translation) error {
	args := m.Called(ctx, t)
	return args.Error(0)
}

func (m *MockTranslationRepository) GetByID(ctx context.Context, id uint) (*domain.Translation, error) {
	args := m.Called(ctx, id)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*domain.Translation), args.Error(1)
}

func (m *MockTranslationRepository) Update(ctx context.Context, t *domain.Translation) error {
	args := m.Called(ctx, t)
	return args.Error(0)
}

func (m *MockTranslationRepository) Delete(ctx context.Context, id uint) error {
	args := m.Called(ctx, id)
	return args.Error(0)
}

func (m *MockTranslationRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Translation], error) {
	args := m.Called(ctx, page, pageSize)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*domain.PaginatedResult[domain.Translation]), args.Error(1)
}

func (m *MockTranslationRepository) ListAll(ctx context.Context) ([]domain.Translation, error) {
	args := m.Called(ctx)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.Translation), args.Error(1)
}

func (m *MockTranslationRepository) ListByWorkIDPaginated(ctx context.Context, workID uint, language *string, page, pageSize int) (*domain.PaginatedResult[domain.Translation], error) {
	args := m.Called(ctx, workID, language, page, pageSize)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*domain.PaginatedResult[domain.Translation]), args.Error(1)
}

func (m *MockTranslationRepository) Count(ctx context.Context) (int64, error) {
	args := m.Called(ctx)
	return args.Get(0).(int64), args.Error(1)
}

func (m *MockTranslationRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*domain.Translation, error) {
	args := m.Called(ctx, preloads, id)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*domain.Translation), args.Error(1)
}

func (m *MockTranslationRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]domain.Translation, error) {
	args := m.Called(ctx, batchSize, offset)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.Translation), args.Error(1)
}

// New BaseRepository methods
func (m *MockTranslationRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Translation) error {
	args := m.Called(ctx, tx, entity)
	return args.Error(0)
}

func (m *MockTranslationRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Translation, error) {
	args := m.Called(ctx, id, options)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*domain.Translation), args.Error(1)
}

func (m *MockTranslationRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Translation) error {
	args := m.Called(ctx, tx, entity)
	return args.Error(0)
}

func (m *MockTranslationRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
	args := m.Called(ctx, tx, id)
	return args.Error(0)
}

func (m *MockTranslationRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.Translation, error) {
	args := m.Called(ctx, options)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.Translation), args.Error(1)
}

func (m *MockTranslationRepository) CountWithOptions(ctx context.Context, options *domain.QueryOptions) (int64, error) {
	args := m.Called(ctx, options)
	return args.Get(0).(int64), args.Error(1)
}

func (m *MockTranslationRepository) Exists(ctx context.Context, id uint) (bool, error) {
	args := m.Called(ctx, id)
	return args.Bool(0), args.Error(1)
}

func (m *MockTranslationRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
	args := m.Called(ctx)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).(*gorm.DB), args.Error(1)
}

func (m *MockTranslationRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
	// This is tricky to mock. For now, just execute the function.
	// A real test might want to mock the transaction itself.
	return fn(nil)
}

func (m *MockTranslationRepository) Upsert(ctx context.Context, t *domain.Translation) error {
	args := m.Called(ctx, t)
	return args.Error(0)
}

// TranslationRepository specific methods
func (m *MockTranslationRepository) ListByWorkID(ctx context.Context, workID uint) ([]domain.Translation, error) {
	args := m.Called(ctx, workID)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.Translation), args.Error(1)
}

func (m *MockTranslationRepository) ListByEntity(ctx context.Context, entityType string, entityID uint) ([]domain.Translation, error) {
	args := m.Called(ctx, entityType, entityID)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.Translation), args.Error(1)
}

func (m *MockTranslationRepository) ListByTranslatorID(ctx context.Context, translatorID uint) ([]domain.Translation, error) {
	args := m.Called(ctx, translatorID)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.Translation), args.Error(1)
}

func (m *MockTranslationRepository) ListByStatus(ctx context.Context, status domain.TranslationStatus) ([]domain.Translation, error) {
	args := m.Called(ctx, status)
	if args.Get(0) == nil {
		return nil, args.Error(1)
	}
	return args.Get(0).([]domain.Translation), args.Error(1)
}
