package role

import (
	"fmt"
	"mini_server/internal/dao"
	"mini_server/internal/server/httpreply"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

// 角色添加

type CreateRequest struct {
	VoiceId       int    `json:"voice_id" bingding:"gt=0"`
	RoleName      string `json:"role_name"`
	WakeupWords   string `json:"wakeup_words" binding:"min=1"`
	WorldScenario string `json:"world_scenario" binding:"min=1"`
	Description   string `json:"description" binding:"min=1"`
	Emojis        string `json:"emojis"` // json格式
	Dialogues     string `json:"dialogues" binding:"min=1"`
	AvatarId      string `json:"avatar_id" binding:"min=0"`      /// 头像图片路径
	BackgroundIds string `json:"background_ids" binding:"min=0"` // 背景图列表，以逗号分隔
}

type CreateReply struct {
	RoleId int32 `json:"role_id"` // 角色ID
}

// 添加角色
func Create(c *gin.Context) {
	reply := httpreply.NewDefaultReplyData()
	defer httpreply.Reply(c, reply)

	var request CreateRequest
	if err := c.ShouldBindJSON(&request); err != nil {
		reply.Status = http.StatusBadRequest
		reply.Message = fmt.Sprintf("invalid json data,err:%v", err)
		return
	}

	// 将数据写入数据库中
	data := dao.Character{
		VoiceId:       request.VoiceId,
		Name:          request.RoleName,
		WakeupWords:   request.WakeupWords,
		WorldScenario: request.WorldScenario,
		Description:   request.Description,
		Emojis:        request.Emojis,
		Dialogues:     request.Dialogues,
		AvatarId:      request.AvatarId,
		BackgroundIds: request.BackgroundIds,
	}

	if data.Emojis == "" {
		data.Emojis = "{}"
	}
	roleId, err := dao.Character_Insert(&data)
	if err != nil {
		logrus.Errorf("dao.Character_Insert failed, err:%v", err)
		reply.Status = http.StatusInternalServerError
		reply.Message = "添加角色失败"
		return
	}
	reply.Data = &CreateReply{RoleId: roleId}
}
