package response

import (
	"encoding/json"
	"time"

	"git.echol.cn/loser/st/server/model/app"
)

// PresetResponse 预设响应
type PresetResponse struct {
	ID                uint                   `json:"id"`
	UserID            uint                   `json:"userId"`
	Name              string                 `json:"name"`
	Description       string                 `json:"description"`
	IsPublic          bool                   `json:"isPublic"`
	IsDefault         bool                   `json:"isDefault"`
	Temperature       float64                `json:"temperature"`
	TopP              float64                `json:"topP"`
	TopK              int                    `json:"topK"`
	FrequencyPenalty  float64                `json:"frequencyPenalty"`
	PresencePenalty   float64                `json:"presencePenalty"`
	MaxTokens         int                    `json:"maxTokens"`
	RepetitionPenalty float64                `json:"repetitionPenalty"`
	MinP              float64                `json:"minP"`
	TopA              float64                `json:"topA"`
	SystemPrompt      string                 `json:"systemPrompt"`
	StopSequences     []string               `json:"stopSequences"`
	Extensions        map[string]interface{} `json:"extensions"`
	UseCount          int                    `json:"useCount"`
	CreatedAt         time.Time              `json:"createdAt"`
	UpdatedAt         time.Time              `json:"updatedAt"`
}

// PresetListResponse 预设列表响应
type PresetListResponse struct {
	List     []PresetResponse `json:"list"`
	Total    int64            `json:"total"`
	Page     int              `json:"page"`
	PageSize int              `json:"pageSize"`
}

// ToPresetResponse 转换为预设响应结构
func ToPresetResponse(preset *app.AIPreset) PresetResponse {
	var stopSequences []string
	if len(preset.StopSequences) > 0 {
		json.Unmarshal(preset.StopSequences, &stopSequences)
	}

	var extensions map[string]interface{}
	if len(preset.Extensions) > 0 {
		json.Unmarshal(preset.Extensions, &extensions)
	}

	return PresetResponse{
		ID:                preset.ID,
		UserID:            preset.UserID,
		Name:              preset.Name,
		Description:       preset.Description,
		IsPublic:          preset.IsPublic,
		IsDefault:         preset.IsDefault,
		Temperature:       preset.Temperature,
		TopP:              preset.TopP,
		TopK:              preset.TopK,
		FrequencyPenalty:  preset.FrequencyPenalty,
		PresencePenalty:   preset.PresencePenalty,
		MaxTokens:         preset.MaxTokens,
		RepetitionPenalty: preset.RepetitionPenalty,
		MinP:              preset.MinP,
		TopA:              preset.TopA,
		SystemPrompt:      preset.SystemPrompt,
		StopSequences:     stopSequences,
		Extensions:        extensions,
		UseCount:          preset.UseCount,
		CreatedAt:         preset.CreatedAt,
		UpdatedAt:         preset.UpdatedAt,
	}
}
