package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"
)

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

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

func (l *GetMyStarsLogic) GetMyStars(in *plant.GetProfileReq) (*plant.UserStarListResp, error) {
	var stars []plantModel.UserStar
	if err := l.svcCtx.DB.Where("user_id = ?", in.UserId).Order("created_at desc").Find(&stars).Error; err != nil {
		return nil, err
	}
	list := make([]*plant.UserStarInfo, 0, len(stars))
	for _, s := range stars {
		list = append(list, &plant.UserStarInfo{
			Id: s.ID, TargetId: s.TargetID, Type: s.Type,
			CreatedAt: s.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}
	return &plant.UserStarListResp{List: list, Total: int64(len(list))}, nil
}
