package logic

import (
	"context"

	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"

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

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

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

// 获取用户资料 (首次访问自动初始化)
func (l *GetUserProfileLogic) GetUserProfile(in *plant.GetProfileReq) (*plant.PlantUserProfile, error) {
	var profile plantModel.UserProfile
	err := l.svcCtx.DB.Where("user_id = ?", in.UserId).FirstOrCreate(&profile, plantModel.UserProfile{
		UserID: in.UserId,
	}).Error
	if err != nil {
		return nil, err
	}
	return &plant.PlantUserProfile{
		Id:              profile.ID,
		UserId:          profile.UserID,
		NickName:        profile.NickName,
		AvatarId:        profile.AvatarID,
		LevelId:         profile.LevelID,
		CurrentSunlight: profile.CurrentSunlight,
		TotalSunlight:   profile.TotalSunlight,
		PlantCount:      profile.PlantCount,
		CareCount:       profile.CareCount,
		PostCount:       profile.PostCount,
		WaterCount:      profile.WaterCount,
		FertilizeCount:  profile.FertilizeCount,
		RepotCount:      profile.RepotCount,
		PruneCount:      profile.PruneCount,
		PhotoCount:      profile.PhotoCount,
	}, nil
}
