package response

import (
	"encoding/json"
	"time"

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

// RegexScriptResponse 正则脚本响应
type RegexScriptResponse struct {
	ID              uint                   `json:"id"`
	UserID          uint                   `json:"userId"`
	Name            string                 `json:"name"`
	FindRegex       string                 `json:"findRegex"`
	ReplaceWith     string                 `json:"replaceWith"`
	TrimStrings     []string               `json:"trimStrings"`
	Placement       int                    `json:"placement"`
	Disabled        bool                   `json:"disabled"`
	MarkdownOnly    bool                   `json:"markdownOnly"`
	RunOnEdit       bool                   `json:"runOnEdit"`
	PromptOnly      bool                   `json:"promptOnly"`
	SubstituteRegex bool                   `json:"substituteRegex"`
	MinDepth        *int                   `json:"minDepth"`
	MaxDepth        *int                   `json:"maxDepth"`
	Scope           int                    `json:"scope"`
	OwnerCharID     *uint                  `json:"ownerCharId"`
	OwnerPresetID   *uint                  `json:"ownerPresetId"`
	Order           int                    `json:"order"`
	Extensions      map[string]interface{} `json:"extensions"`
	CreatedAt       time.Time              `json:"createdAt"`
	UpdatedAt       time.Time              `json:"updatedAt"`
}

// TestRegexScriptResponse 测试正则脚本响应
type TestRegexScriptResponse struct {
	Original string `json:"original"`
	Result   string `json:"result"`
	Success  bool   `json:"success"`
	Error    string `json:"error,omitempty"`
}

// ToRegexScriptResponse 转换为正则脚本响应结构
func ToRegexScriptResponse(script *app.RegexScript) RegexScriptResponse {
	var trimStrings []string
	if len(script.TrimStrings) > 0 {
		json.Unmarshal(script.TrimStrings, &trimStrings)
	}

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

	return RegexScriptResponse{
		ID:              script.ID,
		UserID:          script.UserID,
		Name:            script.Name,
		FindRegex:       script.FindRegex,
		ReplaceWith:     script.ReplaceWith,
		TrimStrings:     trimStrings,
		Placement:       script.Placement,
		Disabled:        script.Disabled,
		MarkdownOnly:    script.MarkdownOnly,
		RunOnEdit:       script.RunOnEdit,
		PromptOnly:      script.PromptOnly,
		SubstituteRegex: script.SubstituteRegex,
		MinDepth:        script.MinDepth,
		MaxDepth:        script.MaxDepth,
		Scope:           script.Scope,
		OwnerCharID:     script.OwnerCharID,
		OwnerPresetID:   script.OwnerPresetID,
		Order:           script.Order,
		Extensions:      extensions,
		CreatedAt:       script.CreatedAt,
		UpdatedAt:       script.UpdatedAt,
	}
}
