package Wx

import (
	"context"
	"wechat-api/ent"
	"wechat-api/ent/contact"
	"wechat-api/ent/wx"
	"wechat-api/internal/utils/dberrorhandler"

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

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

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

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

func (l *GetWxAllowBlockListLogic) GetWxAllowBlockList(req *types.IDReq) (resp *types.AllowBlockListResp, err error) {
	isAdmin := l.ctx.Value("isAdmin").(bool)
	var data *ent.Wx
	if isAdmin {
		data, err = l.svcCtx.DB.Wx.Query().
			Where(
				wx.IDEQ(req.Id),
			).
			Only(l.ctx)
	} else {
		organizationId := l.ctx.Value("organizationId").(uint64)
		data, err = l.svcCtx.DB.Wx.Query().
			Where(
				wx.IDEQ(req.Id),                   // Filter by ID
				wx.OrganizationID(organizationId), // Additional filter by organizationId
			).
			Only(l.ctx)
	}
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}
	wxid := data.Wxid
	resp = &types.AllowBlockListResp{}
	resp.Data = types.AllowBlockListRespData{}

	allowListData := make([]*types.AllowBlockData, 0)
	if data.AllowList == nil || len(data.AllowList) == 0 || data.AllowList[0] == "ALL" {
		allowListData = append(allowListData,
			&types.AllowBlockData{
				Wxid:     "ALL",
				Nickname: "全部允许",
			})
	} else if len(data.AllowList) > 0 {
		allowList, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.AllowList...)).All(l.ctx)
		if allowList != nil {
			for _, av := range allowList {
				allowListData = append(allowListData,
					&types.AllowBlockData{
						Wxid:     av.Wxid,
						Nickname: av.Nickname,
					})
			}
		}
	}
	resp.Data.AllowList = allowListData

	groupAllowListData := make([]*types.AllowBlockData, 0)
	if data.GroupAllowList == nil || len(data.GroupAllowList) == 0 || data.GroupAllowList[0] == "ALL" {
		groupAllowListData = append(groupAllowListData,
			&types.AllowBlockData{
				Wxid:     "ALL",
				Nickname: "全部允许",
			})
	} else if len(data.GroupAllowList) > 0 {
		groupAllowList, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.GroupAllowList...)).All(l.ctx)
		if groupAllowList != nil {
			for _, av := range groupAllowList {
				groupAllowListData = append(groupAllowListData,
					&types.AllowBlockData{
						Wxid:     av.Wxid,
						Nickname: av.Nickname,
					})
			}
		}
	}
	resp.Data.GroupAllowList = groupAllowListData
	blockListData := make([]*types.AllowBlockData, 0)
	if data.BlockList != nil && len(data.BlockList) > 0 {
		if data.BlockList[0] == "ALL" {
			blockListData = append(blockListData,
				&types.AllowBlockData{
					Wxid:     "ALL",
					Nickname: "全部禁用",
				})
		} else {
			blockList, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.BlockList...)).All(l.ctx)
			if blockList != nil {
				for _, av := range blockList {
					blockListData = append(blockListData,
						&types.AllowBlockData{
							Wxid:     av.Wxid,
							Nickname: av.Nickname,
						})
				}
			}
		}
	}
	resp.Data.BlockList = blockListData

	groupBlockListData := make([]*types.AllowBlockData, 0)
	if data.GroupBlockList != nil && len(data.GroupBlockList) > 0 {
		if data.GroupBlockList[0] == "ALL" {
			groupBlockListData = append(groupBlockListData,
				&types.AllowBlockData{
					Wxid:     "ALL",
					Nickname: "全部禁用",
				})
		} else {
			groupBlockList, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.GroupBlockList...)).All(l.ctx)
			if groupBlockList != nil {
				for _, av := range groupBlockList {
					groupBlockListData = append(groupBlockListData,
						&types.AllowBlockData{
							Wxid:     av.Wxid,
							Nickname: av.Nickname,
						})
				}
			}
		}
	}
	resp.Data.GroupBlockList = groupBlockListData
	return resp, nil
}
