package response

import (
	"encoding/json"
	"time"

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

// CharacterResponse 角色卡响应
type CharacterResponse struct {
	ID                      uint                   `json:"id"`
	Name                    string                 `json:"name"`
	Avatar                  string                 `json:"avatar"`
	Creator                 string                 `json:"creator"`
	Version                 string                 `json:"version"`
	Description             string                 `json:"description"`
	Personality             string                 `json:"personality"`
	Scenario                string                 `json:"scenario"`
	FirstMes                string                 `json:"firstMes"`
	MesExample              string                 `json:"mesExample"`
	CreatorNotes            string                 `json:"creatorNotes"`
	SystemPrompt            string                 `json:"systemPrompt"`
	PostHistoryInstructions string                 `json:"postHistoryInstructions"`
	Tags                    []string               `json:"tags"`
	AlternateGreetings      []string               `json:"alternateGreetings"`
	CharacterBook           map[string]interface{} `json:"characterBook"`
	Extensions              map[string]interface{} `json:"extensions"`
	Spec                    string                 `json:"spec"`
	SpecVersion             string                 `json:"specVersion"`
	IsPublic                bool                   `json:"isPublic"`
	UseCount                int                    `json:"useCount"`
	FavoriteCount           int                    `json:"favoriteCount"`
	CreatedAt               time.Time              `json:"createdAt"`
	UpdatedAt               time.Time              `json:"updatedAt"`
}

// CharacterSimpleResponse 角色卡简化响应（用于列表）
type CharacterSimpleResponse struct {
	ID          uint      `json:"id"`
	Name        string    `json:"name"`
	Avatar      string    `json:"avatar"`
	Description string    `json:"description"`
	CreatedAt   time.Time `json:"createdAt"`
	UpdatedAt   time.Time `json:"updatedAt"`
}

// CharacterListResponse 角色卡列表响应
type CharacterListResponse struct {
	List     []CharacterResponse `json:"list"`
	Total    int64               `json:"total"`
	Page     int                 `json:"page"`
	PageSize int                 `json:"pageSize"`
}

// ToCharacterResponse 转换为角色卡响应结构
func ToCharacterResponse(character *app.AICharacter) CharacterResponse {
	resp := CharacterResponse{
		ID:                      character.ID,
		Name:                    character.Name,
		Avatar:                  character.Avatar,
		Creator:                 character.Creator,
		Version:                 character.Version,
		Description:             character.Description,
		Personality:             character.Personality,
		Scenario:                character.Scenario,
		FirstMes:                character.FirstMes,
		MesExample:              character.MesExample,
		CreatorNotes:            character.CreatorNotes,
		SystemPrompt:            character.SystemPrompt,
		PostHistoryInstructions: character.PostHistoryInstructions,
		Spec:                    character.Spec,
		SpecVersion:             character.SpecVersion,
		IsPublic:                character.IsPublic,
		UseCount:                character.UseCount,
		FavoriteCount:           character.FavoriteCount,
		CreatedAt:               character.CreatedAt,
		UpdatedAt:               character.UpdatedAt,
	}

	// 解析 JSON 字段
	if len(character.Tags) > 0 {
		json.Unmarshal(character.Tags, &resp.Tags)
	}
	if len(character.AlternateGreetings) > 0 {
		json.Unmarshal(character.AlternateGreetings, &resp.AlternateGreetings)
	}
	if len(character.CharacterBook) > 0 {
		json.Unmarshal(character.CharacterBook, &resp.CharacterBook)
	}
	if len(character.Extensions) > 0 {
		json.Unmarshal(character.Extensions, &resp.Extensions)
	}

	return resp
}

// ToCharacterSimpleResponse 转换为角色卡简化响应结构（用于列表）
func ToCharacterSimpleResponse(character *app.AICharacter) CharacterSimpleResponse {
	return CharacterSimpleResponse{
		ID:          character.ID,
		Name:        character.Name,
		Avatar:      character.Avatar,
		Description: character.Description,
		CreatedAt:   character.CreatedAt,
		UpdatedAt:   character.UpdatedAt,
	}
}
