package manager

import (
	"chainweaver.org.cn/chainweaver/did/did-mgr-common-service/errorcode"
	"chainweaver.org.cn/chainweaver/did/did-mgr-common-service/internal/db"
	"context"

	"chainweaver.org.cn/chainweaver/did/did-mgr-common-service/internal/svc"
	"chainweaver.org.cn/chainweaver/did/did-mgr-common-service/internal/types"

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

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

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

type NotificationInfosResp struct {
	Page types.Page             `json:"page"`
	List []*db.NotificationInfo `json:"list"`
}

func (l *NotificationQueryLogic) NotificationQuery(req *types.PageReq) (resp *types.CommonResp, err error) {
	resp = &types.CommonResp{
		Code: types.SucceedCode,
		Msg:  types.SucceedMsg,
	}

	if req.Page.Size == 0 {
		req.Page.Size = 10
	}

	offset := req.Page.Size * (req.Page.Page - 1)
	limit := req.Page.Size
	if req.Page.Page == 0 {
		limit = 10
		offset = 0
	}

	nInfos, count, err := db.GetNotificationInfos(offset, limit)
	if err != nil {
		errorcode.MysqlErrorQuery.BuildResult(resp, err)
		l.Logger.Errorf("query NotificationInfos failed,err[%s]", err.Error())
		return resp, nil
	}

	resp.Data = &NotificationInfosResp{
		Page: types.Page{
			Total: int(count),
			Page:  req.Page.Page,
		},
		List: nInfos,
	}
	return resp, nil
}