package contact

import (
	"context"
	"wechat-api/ent"
	"wechat-api/ent/label"
	"wechat-api/ent/labelrelationship"
	"wechat-api/ent/wx"

	"wechat-api/ent/contact"
	"wechat-api/ent/predicate"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"

	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func convertLabelToLabelInfo(label *ent.Label) types.LabelInfo {
	return types.LabelInfo{
		BaseIDInfo: types.BaseIDInfo{
			Id:        &label.ID,
			CreatedAt: pointy.GetPointer(label.CreatedAt.UnixMilli()),
			UpdatedAt: pointy.GetPointer(label.UpdatedAt.UnixMilli()),
		},
		Status:     &label.Status,
		Type:       &label.Type,
		Name:       &label.Name,
		From:       &label.From,
		Mode:       &label.Mode,
		Conditions: &label.Conditions,
	}
}

func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.ContactListResp, error) {
	organizationId := l.ctx.Value("organizationId").(uint64)
	var predicates []predicate.Contact
	predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
	if len(req.LabelIDs) > 0 {
		predicates = append(predicates, contact.HasContactRelationshipsWith(
			labelrelationship.HasLabelsWith(
				label.IDIn(req.LabelIDs...),
			),
			labelrelationship.DeletedAtIsNil(),
		))
	}
	if req.WxWxid != nil {
		predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
	}
	if req.Wxid != nil {
		predicates = append(predicates, contact.WxidContains(*req.Wxid))
	}
	if req.Account != nil {
		predicates = append(predicates, contact.AccountContains(*req.Account))
	}
	if req.Nickname != nil {
		predicates = append(predicates, contact.NicknameContains(*req.Nickname))
	}
	if req.Type != nil {
		predicates = append(predicates, contact.TypeEQ(*req.Type))
	} else {
		predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
	}
	data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
		query.WithLabels()
	}).Page(l.ctx, req.Page, req.PageSize)

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

	resp := &types.ContactListResp{}
	resp.Msg = errormsg.Success
	resp.Data.Total = data.PageDetails.Total

	wxWxids := []string{}
	wxWxidsSet := make(map[string]*ent.Wx)
	blockListSet := make(map[string]struct{})
	groupBlockListSet := make(map[string]struct{})
	for _, v := range data.List {
		wxWxids = append(wxWxids, v.WxWxid)
	}
	wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
	for _, w := range wxs {
		wxWxidsSet[w.Wxid] = w
		for _, b := range w.BlockList {
			blockListSet[w.Wxid+"_"+b] = struct{}{}
		}
		for _, g := range w.GroupBlockList {
			groupBlockListSet[w.Wxid+"_"+g] = struct{}{}
		}
	}

	for _, v := range data.List {
		isInBlockList := false
		labelRelationships := make([]types.ContactLabelList, 0)
		if v.Edges.ContactRelationships != nil {
			for _, lr := range v.Edges.ContactRelationships {
				if lr.Edges.Labels == nil {
					continue
				}
				labelRelationships = append(labelRelationships, types.ContactLabelList{
					Label: &lr.Edges.Labels.Name,
					Value: &lr.LabelID,
				})
			}
		}
		var wxNickname string
		if wxWxidsSet[v.WxWxid] == nil {
			wxNickname = v.WxWxid
		} else {
			wxNickname = wxWxidsSet[v.WxWxid].Nickname
		}
		if v.Type == 1 {
			if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
				isInBlockList = true
			} else {
				_, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
			}
		} else {
			if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
				isInBlockList = true
			} else {
				_, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
			}
		}

		resp.Data.Data = append(resp.Data.Data,
			types.ContactInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id:        &v.ID,
					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
				},
				Status:             &v.Status,
				WxWxid:             &v.WxWxid,
				WxWxidNickname:     &wxNickname,
				Type:               &v.Type,
				Wxid:               &v.Wxid,
				Account:            &v.Account,
				Nickname:           &v.Nickname,
				Markname:           &v.Markname,
				Headimg:            &v.Headimg,
				Sex:                &v.Sex,
				Starrole:           &v.Starrole,
				Dontseeit:          &v.Dontseeit,
				Dontseeme:          &v.Dontseeme,
				Lag:                &v.Lag,
				Gid:                &v.Gid,
				Gname:              &v.Gname,
				V3:                 &v.V3,
				LabelRelationships: labelRelationships,
				IsInBlockList:      &isInBlockList,
			})
	}

	return resp, nil
}
