package ai

import (
	"haixun-backend/internal/model/ai/domain/enum"
	domai "haixun-backend/internal/model/ai/domain/usecase"
	threadsdom "haixun-backend/internal/model/threads_account/domain/usecase"
	"haixun-backend/internal/types"
)

func toCredentialGenerateRequest(
	messages []types.AIMessage,
	credential *threadsdom.WorkerAiCredential,
	system string,
) domai.GenerateRequest {
	domMessages := make([]domai.Message, 0, len(messages))
	for _, msg := range messages {
		domMessages = append(domMessages, domai.Message{
			Role:    msg.Role,
			Content: msg.Content,
		})
	}
	provider := ""
	model := ""
	apiKey := ""
	if credential != nil {
		provider = credential.Provider
		model = credential.Model
		apiKey = credential.APIKey
	}
	return domai.GenerateRequest{
		Provider: enum.ProviderID(provider),
		Model:    model,
		Credential: domai.Credential{
			APIKey: apiKey,
		},
		System:   system,
		Messages: domMessages,
	}
}

func toGenerateRequest(req *types.AIChatReq, token string, system string) domai.GenerateRequest {
	messages := make([]domai.Message, 0, len(req.Messages))
	for _, msg := range req.Messages {
		messages = append(messages, domai.Message{
			Role:    msg.Role,
			Content: msg.Content,
		})
	}
	return domai.GenerateRequest{
		Provider: enum.ProviderID(req.Provider),
		Model:    req.Model,
		Credential: domai.Credential{
			APIKey: token,
		},
		System:      system,
		Messages:    messages,
		Temperature: req.Temperature,
		MaxTokens:   req.MaxTokens,
	}
}
