package response

import (
	"encoding/json"
	"time"

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

// WorldbookResponse 世界书响应
type WorldbookResponse struct {
	ID          uint      `json:"id"`
	UserID      uint      `json:"userId"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	IsPublic    bool      `json:"isPublic"`
	EntryCount  int       `json:"entryCount"`
	CreatedAt   time.Time `json:"createdAt"`
	UpdatedAt   time.Time `json:"updatedAt"`
}

// WorldbookListResponse 世界书列表响应
type WorldbookListResponse struct {
	List     []WorldbookResponse `json:"list"`
	Total    int64               `json:"total"`
	Page     int                 `json:"page"`
	PageSize int                 `json:"pageSize"`
}

// EntryResponse 世界书条目响应
type EntryResponse struct {
	ID              uint                   `json:"id"`
	WorldbookID     uint                   `json:"worldbookId"`
	Comment         string                 `json:"comment"`
	Content         string                 `json:"content"`
	Keys            []string               `json:"keys"`
	SecondaryKeys   []string               `json:"secondaryKeys"`
	Constant        bool                   `json:"constant"`
	Enabled         bool                   `json:"enabled"`
	UseRegex        bool                   `json:"useRegex"`
	CaseSensitive   bool                   `json:"caseSensitive"`
	MatchWholeWords bool                   `json:"matchWholeWords"`
	Selective       bool                   `json:"selective"`
	SelectiveLogic  int                    `json:"selectiveLogic"`
	Position        int                    `json:"position"`
	Depth           int                    `json:"depth"`
	Order           int                    `json:"order"`
	Probability     int                    `json:"probability"`
	ScanDepth       int                    `json:"scanDepth"`
	GroupID         string                 `json:"groupId"`
	Extensions      map[string]interface{} `json:"extensions"`
	CreatedAt       time.Time              `json:"createdAt"`
	UpdatedAt       time.Time              `json:"updatedAt"`
}

// EntryListResponse 条目列表响应
type EntryListResponse struct {
	List  []EntryResponse `json:"list"`
	Total int64           `json:"total"`
}

// ToWorldbookResponse 转换为世界书响应结构
func ToWorldbookResponse(wb *app.Worldbook) WorldbookResponse {
	return WorldbookResponse{
		ID:          wb.ID,
		UserID:      wb.UserID,
		Name:        wb.Name,
		Description: wb.Description,
		IsPublic:    wb.IsPublic,
		EntryCount:  wb.EntryCount,
		CreatedAt:   wb.CreatedAt,
		UpdatedAt:   wb.UpdatedAt,
	}
}

// ToEntryResponse 转换为条目响应结构
func ToEntryResponse(entry *app.WorldbookEntry) EntryResponse {
	var keys []string
	if len(entry.Keys) > 0 {
		json.Unmarshal(entry.Keys, &keys)
	}
	var secondaryKeys []string
	if len(entry.SecondaryKeys) > 0 {
		json.Unmarshal(entry.SecondaryKeys, &secondaryKeys)
	}
	var extensions map[string]interface{}
	if len(entry.Extensions) > 0 {
		json.Unmarshal(entry.Extensions, &extensions)
	}
	return EntryResponse{
		ID:              entry.ID,
		WorldbookID:     entry.WorldbookID,
		Comment:         entry.Comment,
		Content:         entry.Content,
		Keys:            keys,
		SecondaryKeys:   secondaryKeys,
		Constant:        entry.Constant,
		Enabled:         entry.Enabled,
		UseRegex:        entry.UseRegex,
		CaseSensitive:   entry.CaseSensitive,
		MatchWholeWords: entry.MatchWholeWords,
		Selective:       entry.Selective,
		SelectiveLogic:  entry.SelectiveLogic,
		Position:        entry.Position,
		Depth:           entry.Depth,
		Order:           entry.Order,
		Probability:     entry.Probability,
		ScanDepth:       entry.ScanDepth,
		GroupID:         entry.GroupID,
		Extensions:      extensions,
		CreatedAt:       entry.CreatedAt,
		UpdatedAt:       entry.UpdatedAt,
	}
}
