// Package asChat -----------------------------
// @file      : dto.go
// @author    : JJXu
// @contact   : wavingbear@163.com
// @time      : 2024/9/10 下午6:28
// -------------------------------------------
package dto

import (
	"encoding/json"
	"fonchain-fiee/api/accountFiee"
	"log"
	"time"
)

type Message struct {
	MsgType    accountFiee.MsgType `json:"msgType"`
	Text       string              `json:"text"` //文本内容
	Media      []MessageMedia      `json:"media"`
	LocalStamp int64               `json:"localStamp"`
}
type MessageMedia struct {
	MediaId   int64  `json:"mediaId"`   //媒体文件id
	MediaSize string `json:"mediaSize"` //媒体文件大小
	Ext       string `json:"ext"`       //后缀格式
	Url       string `json:"url"`       //文件地址
	ConvText  string `json:"convText"`  //语音转文字内容,需要调用语音转文字接口后才会有值
	Duration  int64  `json:"duration"`  //时长 单位:毫秒
}

// 客户端发送消息请求,使用api发送消息
type NewMessageRequest struct {
	Waiter    bool   `json:"waiter"` //是否是客服发送,客服没有userId
	SessionId string `json:"sessionId"`
	Message
}

// 服务端接收到消息后,使用websocket发送给userId关联的客户端,通知客户端有新消息,然后调用接口获取消息
type NewMessageNotice struct {
	Name      string `json:"name"`   //名字
	UserId    int64  `json:"userId"` //用户id
	SessionId string `json:"sessionId"`
	MessageId int64  `json:"messageId"` //消息id
	//NewMsgTotal int64  `json:"newMsgTotal"` //新消息数量
	//Active      bool   `json:"active"`      //是否在线
}

// 获取会话列表
type SessionType struct {
	NewMessageNotice
	RecentMessage []*Message `json:"recentMessage"` //最近消息
}

type MessageListType struct {
	ID        int64   `json:"ID"`
	CreatedAt string  `json:"createdAt"`
	UserId    int64   `json:"userId"`
	Name      string  `json:"name"`
	Message   Message `json:"message"`
}

func (m *MessageListType) BuildMessage(data *accountFiee.ChatRecordData) {
	m.ID = data.ID
	m.CreatedAt = data.CreatedAt
	m.UserId = data.UserId
	m.Name = data.Name
	switch data.MsgType {
	case accountFiee.MsgType_TextMsgType:
		m.Message = Message{
			MsgType:    accountFiee.MsgType_TextMsgType,
			Text:       data.Content,
			Media:      []MessageMedia{},
			LocalStamp: data.LocalStamp,
		}
	case accountFiee.MsgType_ImageMsgType, accountFiee.MsgType_AudioMsgType, accountFiee.MsgType_VideoMsgType:
		m.Message.MsgType = data.MsgType
		m.Message.Text = data.Content
		m.Message.LocalStamp = data.LocalStamp
		if data.Medias != nil {
			for _, media := range data.Medias {
				m.Message.Media = append(m.Message.Media, MessageMedia{
					MediaId:   media.ID,
					MediaSize: media.Size,
					Ext:       media.Ext,
					Url:       media.Url,
					ConvText:  media.ConvText,
					Duration:  media.Duration,
				})
			}
		}
	}
}
func (m *MessageListType) ToJson() string {
	jsonBytes, _ := json.Marshal(m)
	return string(jsonBytes)
}

type UserMsgStatic struct {
	UserId    int64  `json:"userId"` //用户id
	Name      string `json:"name"`   //名称
	ArtistUid string `json:"artistUid,omitempty"`
	SessionId string `json:"sessionId"` //会话id
	Total     int64  `json:"total"`     //新消息数量
	//NewMessageTime string `json:"newMessageTime"` //最新消息的创建时间
}

type MessageListRequest struct {
	SessionId string        `json:"sessionId"` //不传则获取自己的会话消息里列表
	CurrentId int64         `json:"currentId"` //组合查询条件1:基于某个消息id,向前或向后查找。两种组合条件不能同时使用
	Direction int           `json:"direction"` //组合查询条件1:方向 1=向前查找 2=向后查找
	Recent    bool          `json:"recent"`    //组合查询条件2:查找最新的若干条消息。两种组合条件不能同时使用
	InHour    time.Duration `json:"inHour"`    //组合查询条件2:可选,查询指定小时内的数据
	PageSize  int64         `json:"pageSize"`  //查找数量
}

type VoiceToTextRequest struct {
	MediaId int64 `json:"mediaId"`
}

type ArtistInfoRequest struct {
	UserId int64 `json:"userId"`
}
type ArtistInfo struct {
	Tnum        string `json:"tnum"`
	ArtistName  string `json:"artistName"`
	Age         int64  `json:"age"`
	Sex         string `json:"sex"`
	NativePlace string `json:"nativePlace"`
	TelNum      string `json:"telNum"`
	RecentPhoto string `json:"recentPhoto"`
}

type GetChatAutoReplyRulerListRequest struct {
	Page     int64 `json:"page"`
	PageSize int64 `json:"pageSize"`
	accountFiee.ChatAutoReplyRulerData
}
type ErpLoginDemoReq struct {
	TelNum   string `json:"telNum"`
	Password string `json:"password"`
	Nickname string `json:"nickname"`
	RealName string `json:"realName"`
}

type ChatAutoReplyData struct {
	ID        int64                     `json:"id"`
	Title     string                    `json:"title"`
	Rules     map[string]*AutoReplyRule `json:"rules"`
	Response  string                    `json:"response"`
	CreatedAt string                    `json:"createdAt"`
	UpdatedAt string                    `json:"updatedAt"`
	Status    int32                     `json:"status"`
}

func (r *ChatAutoReplyData) ToProtoData() (data *accountFiee.ChatAutoReplyRulerData) {
	jsonBytes, _ := json.Marshal(r.Rules)
	data = &accountFiee.ChatAutoReplyRulerData{
		ID:        r.ID,
		CreatedAt: r.CreatedAt,
		UpdatedAt: r.UpdatedAt,
		Title:     r.Title,
		Ruler:     string(jsonBytes),
		Status:    r.Status,
		Response:  r.Response,
	}
	return
}
func (r *ChatAutoReplyData) Parse(data *accountFiee.ChatAutoReplyRulerData) {
	err := json.Unmarshal([]byte(data.Ruler), &r.Rules)
	log.Printf("ChatAutoReplyData parse err:%v\n", err)
	r.ID = data.ID
	r.CreatedAt = data.CreatedAt
	r.UpdatedAt = data.UpdatedAt
	r.Title = data.Title
	r.Status = data.Status
	r.Response = data.Response
}

type AutoReplyRule struct {
	Enable         bool          `json:"enable"`
	Content        string        `json:"content,omitempty"`
	SecondDuration time.Duration `json:"secondDuration,omitempty"`
}

type UserDetailReq struct {
	ChatUserId int64 `json:"chatUserId"` //注意是聊天用户ID(chat_user表),不是账号服务ID
}
type UserDetailResp struct {
	UserId      uint64 `json:"userId"`
	ChatUserId  int64  `json:"chatUserId"`
	RnStatus    int32  `json:"rnStatus"` //状态 1:未实名 2:审核中 3:审核失败 4:审核通过
	SubNum      string `json:"subNum"`
	RealName    string `json:"realName"`
	Age         string `json:"age"`
	Gender      string `json:"gender"`
	NativePlace string `json:"nativePlace"`
	Phone       string `json:"phone"`
	GroupPhoto  string `json:"groupPhoto"`
}