package logic

import (
	"context"
	"time"

	"juwan-backend/app/notification/rpc/internal/svc"
	"juwan-backend/app/notification/rpc/pb"

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

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

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

func (l *UpdateNotificationsLogic) UpdateNotifications(in *pb.UpdateNotificationsReq) (*pb.UpdateNotificationsResp, error) {
	updater := l.svcCtx.NotificationModelRW.Notifications.UpdateOneID(in.GetId())
	if in.Read != nil {
		updater = updater.SetRead(in.GetRead())
	}
	if in.Type != nil {
		updater = updater.SetType(in.GetType())
	}
	if in.Title != nil {
		updater = updater.SetTitle(in.GetTitle())
	}
	if in.Content != nil {
		updater = updater.SetContent(in.GetContent())
	}
	if in.Link != nil {
		updater = updater.SetLink(in.GetLink())
	}
	if in.UpdatedAt != nil {
		updater = updater.SetUpdatedAt(time.Unix(in.GetUpdatedAt(), 0))
	}

	_, err := updater.Save(l.ctx)
	if err != nil {
		logx.Errorf("updateNotifications err: %v", err)
		return nil, err
	}

	return &pb.UpdateNotificationsResp{}, nil
}
