package logic

import (
	"context"
	"time"

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

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

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

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

func (l *UpdateReviewsLogic) UpdateReviews(in *pb.UpdateReviewsReq) (*pb.UpdateReviewsResp, error) {
	updater := l.svcCtx.ReviewModelRW.Reviews.UpdateOneID(in.GetId())

	if in.Sealed != nil {
		updater = updater.SetSealed(in.GetSealed())
	}
	if in.UnsealedAt != nil {
		updater = updater.SetUnsealedAt(time.Unix(in.GetUnsealedAt(), 0))
	}
	if in.Rating != nil {
		updater = updater.SetRating(int16(in.GetRating()))
	}
	if in.Content != nil {
		updater = updater.SetContent(in.GetContent())
	}

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