package service

import (
	"testing"

	"nex/backend/internal/domain"
	"nex/backend/internal/repository"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uber.org/zap"
)

func TestProviderService_Update(t *testing.T) {
	db := setupServiceTestDB(t)
	repo := repository.NewProviderRepository(db)
	modelRepo := repository.NewModelRepository(db)
	cache := setupRoutingCache(t, db)
	svc := NewProviderService(repo, modelRepo, cache)

	require.NoError(t, svc.Create(&domain.Provider{ID: "p1", Name: "Original", APIKey: "key", BaseURL: "https://test.com"}))

	err := svc.Update("p1", map[string]interface{}{"name": "Updated"})
	require.NoError(t, err)

	result, err := svc.Get("p1")
	require.NoError(t, err)
	assert.Equal(t, "Updated", result.Name)
}

func TestProviderService_Update_NotFound(t *testing.T) {
	db := setupServiceTestDB(t)
	repo := repository.NewProviderRepository(db)
	modelRepo := repository.NewModelRepository(db)
	cache := setupRoutingCache(t, db)
	svc := NewProviderService(repo, modelRepo, cache)

	err := svc.Update("nonexistent", map[string]interface{}{"name": "test"})
	assert.Error(t, err)
}

func TestModelService_Get(t *testing.T) {
	db := setupServiceTestDB(t)
	providerRepo := repository.NewProviderRepository(db)
	modelRepo := repository.NewModelRepository(db)
	cache := setupRoutingCache(t, db)
	svc := NewModelService(modelRepo, providerRepo, cache)

	require.NoError(t, providerRepo.Create(&domain.Provider{ID: "p1", Name: "P1", APIKey: "key", BaseURL: "https://test.com"}))
	model := &domain.Model{ProviderID: "p1", ModelName: "gpt-4"}
	require.NoError(t, svc.Create(model))

	result, err := svc.Get(model.ID)
	require.NoError(t, err)
	assert.Equal(t, "gpt-4", result.ModelName)
}

func TestModelService_Update(t *testing.T) {
	db := setupServiceTestDB(t)
	providerRepo := repository.NewProviderRepository(db)
	modelRepo := repository.NewModelRepository(db)
	cache := setupRoutingCache(t, db)
	svc := NewModelService(modelRepo, providerRepo, cache)

	require.NoError(t, providerRepo.Create(&domain.Provider{ID: "p1", Name: "P1", APIKey: "key", BaseURL: "https://test.com"}))
	model := &domain.Model{ProviderID: "p1", ModelName: "gpt-4"}
	require.NoError(t, svc.Create(model))

	err := svc.Update(model.ID, map[string]interface{}{"model_name": "gpt-4o"})
	require.NoError(t, err)

	result, err := svc.Get(model.ID)
	require.NoError(t, err)
	assert.Equal(t, "gpt-4o", result.ModelName)
}

func TestModelService_Update_ProviderID_Invalid(t *testing.T) {
	db := setupServiceTestDB(t)
	providerRepo := repository.NewProviderRepository(db)
	modelRepo := repository.NewModelRepository(db)
	cache := setupRoutingCache(t, db)
	svc := NewModelService(modelRepo, providerRepo, cache)

	require.NoError(t, providerRepo.Create(&domain.Provider{ID: "p1", Name: "P1", APIKey: "key", BaseURL: "https://test.com"}))
	model := &domain.Model{ProviderID: "p1", ModelName: "gpt-4"}
	require.NoError(t, svc.Create(model))

	err := svc.Update(model.ID, map[string]interface{}{"provider_id": "nonexistent"})
	assert.Error(t, err)
}

func TestModelService_Delete(t *testing.T) {
	db := setupServiceTestDB(t)
	providerRepo := repository.NewProviderRepository(db)
	modelRepo := repository.NewModelRepository(db)
	cache := setupRoutingCache(t, db)
	svc := NewModelService(modelRepo, providerRepo, cache)

	require.NoError(t, providerRepo.Create(&domain.Provider{ID: "p1", Name: "P1", APIKey: "key", BaseURL: "https://test.com"}))
	model := &domain.Model{ProviderID: "p1", ModelName: "gpt-4"}
	require.NoError(t, svc.Create(model))

	err := svc.Delete(model.ID)
	require.NoError(t, err)

	_, err = svc.Get(model.ID)
	assert.Error(t, err)
}

func TestModelService_Delete_NotFound(t *testing.T) {
	db := setupServiceTestDB(t)
	modelRepo := repository.NewModelRepository(db)
	providerRepo := repository.NewProviderRepository(db)
	cache := setupRoutingCache(t, db)
	svc := NewModelService(modelRepo, providerRepo, cache)

	err := svc.Delete("nonexistent")
	assert.Error(t, err)
}

func TestStatsService_Aggregate_Default(t *testing.T) {
	statsRepo := repository.NewStatsRepository(nil)
	buffer := NewStatsBuffer(statsRepo, zap.NewNop())
	svc := NewStatsService(statsRepo, buffer)

	stats := []domain.UsageStats{
		{ProviderID: "p1", RequestCount: 10},
		{ProviderID: "p2", RequestCount: 5},
	}

	result := svc.Aggregate(stats, "")
	assert.Len(t, result, 2)

	totalCount := 0
	for _, r := range result {
		count, ok := r["request_count"].(int)
		require.True(t, ok)
		totalCount += count
	}
	assert.Equal(t, 15, totalCount)
}

func TestModelService_Update_NotFound(t *testing.T) {
	db := setupServiceTestDB(t)
	modelRepo := repository.NewModelRepository(db)
	providerRepo := repository.NewProviderRepository(db)
	cache := setupRoutingCache(t, db)
	svc := NewModelService(modelRepo, providerRepo, cache)

	err := svc.Update("nonexistent", map[string]interface{}{"model_name": "test"})
	assert.Error(t, err)
}
