package logic

import (
	"context"
	"errors"
	"time"

	"juwan-backend/app/community/rpc/internal/models/posts"
	"juwan-backend/app/community/rpc/internal/svc"
	"juwan-backend/app/community/rpc/pb"

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

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

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

func (l *DelPostsLogic) DelPosts(in *pb.DelPostsReq) (*pb.DelPostsResp, error) {
	if in.GetId() <= 0 {
		return nil, errors.New("id is required")
	}

	now := time.Now()
	_, err := l.svcCtx.CommunityModelRW.Posts.Update().
		Where(posts.IDEQ(in.GetId()), posts.DeletedAtIsNil()).
		SetDeletedAt(now).
		SetUpdatedAt(now).
		Save(l.ctx)
	if err != nil {
		logx.Errorf("delPosts err: %v", err)
	}

	return &pb.DelPostsResp{}, nil
}
