package chatsession

import (
	"context"
	"fmt"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"wechat-api/ent/chatsession"
	"wechat-api/ent/employee"
	"wechat-api/ent/predicate"
	"wechat-api/ent/wxcard"
	"wechat-api/ent/wxcarduser"
	"wechat-api/internal/utils/dberrorhandler"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetChatSessionListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetChatSessionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetChatSessionListLogic {
	return &GetChatSessionListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *GetChatSessionListLogic) GetChatSessionList(req *types.ChatSessionListReq) (*types.ChatSessionListResp, error) {
	resp := &types.ChatSessionListResp{}
	resp.Msg = errormsg.Success

	//organizationId := l.ctx.Value("organizationId").(uint64)
	//isAdmin := l.ctx.Value("isAdmin").(bool)

	var predicates []predicate.ChatSession
	//if isAdmin {
	//
	//} else {
	//	predicates = append(predicates, chatsession.BotType(uint8(2)))
	//	predicates = append(predicates, chatsession.BotID(organizationId))
	//}

	if req.BotType != nil && *req.BotType > 0 {
		predicates = append(predicates, chatsession.BotType(*req.BotType))
	}

	if req.BotId != nil && *req.BotId > 0 {
		predicates = append(predicates, chatsession.BotID(*req.BotId))
	}

	//if req.BotType != nil && *req.BotType > 0 {
	//	predicates = append(predicates, chatsession.BotType(*req.BotType))
	//}
	//if req.BotName != nil && *req.BotName != "" {
	//	predicates = append(predicates, chatsession.BotType(*req.BotType))
	//
	//	ids := make([]uint64, 0)
	//	if *req.BotType == 2 {
	//		wxcardList, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.NameContains(*req.BotName)).All(l.ctx)
	//		if err != nil {
	//			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	//		}
	//		for _, val := range wxcardList {
	//			ids = append(ids, val.ID)
	//		}
	//	} else if *req.BotType == 3 {
	//		employeeList, err := l.svcCtx.DB.Employee.Query().Where(employee.TitleContains(*req.BotName)).All(l.ctx)
	//		if err != nil {
	//			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	//		}
	//		for _, val := range employeeList {
	//			ids = append(ids, val.ID)
	//		}
	//	}
	//
	//	if len(ids) > 0 {
	//		predicates = append(predicates, chatsession.BotIDIn(ids...))
	//	} else {
	//		return resp, nil
	//	}
	//}

	data, err := l.svcCtx.DB.ChatSession.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	resp.Data.Total = data.PageDetails.Total

	for _, v := range data.List {
		botType := ""
		botName := ""
		userName := ""
		if v.BotType == 1 {
			botType = "微信"
		} else if v.BotType == 2 {
			botType = "名片"
			cardInfo, _ := l.svcCtx.DB.WxCard.Query().Where(wxcard.ID(v.BotID)).First(l.ctx)
			botName = cardInfo.Name
		} else if v.BotType == 3 {
			botType = "数字员工"
			employeeInfo, _ := l.svcCtx.DB.Employee.Query().Where(employee.ID(v.BotID)).First(l.ctx)
			botName = employeeInfo.Title
		}
		userInfo, _ := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(v.UserID)).First(l.ctx)
		if userInfo != nil {
			userName = userInfo.Nickname
		} else {
			userName = fmt.Sprintf("用户_%d", v.UserID)
		}
		resp.Data.Data = append(resp.Data.Data,
			types.ChatSessionInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id:        &v.ID,
					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
				},
				Name:       &v.Name,
				UserId:     &v.UserID,
				UserName:   &userName,
				BotId:      &v.BotID,
				BotName:    &botName,
				BotType:    &v.BotType,
				BotTypeStr: &botType,
			})
	}

	return resp, nil
}
