package logic

import (
	"context"
	"fmt"

	sysModel "sundynix-micro-go/app/system/model"
	"sundynix-micro-go/app/system/rpc/internal/svc"
	"sundynix-micro-go/app/system/rpc/system"

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

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

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

func (l *GetUserDetailLogic) GetUserDetail(in *system.GetUserDetailReq) (*system.UserDetailResp, error) {
	var user sysModel.SundynixUser
	if err := l.svcCtx.DB.Preload("Roles").Where("id = ?", in.Id).First(&user).Error; err != nil {
		return nil, fmt.Errorf("获取用户详情失败: %w", err)
	}

	var roleIds []string
	for _, role := range user.Roles {
		roleIds = append(roleIds, role.ID)
	}

	return &system.UserDetailResp{
		Id:       user.ID,
		Name:     user.Name,
		Account:  user.Account,
		NickName: user.NickName,
		Phone:    user.Phone,
		RoleIds:  roleIds,
	}, nil
}
