package health

import (
	"context"
	"fmt"

	"apps/backend/internal/logic/growthmap"
	"apps/backend/internal/middleware"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *GetAccountHealthLogic) GetAccountHealth(req *types.GetAccountHealthReq) (*types.AccountHealthPublic, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	h, err := l.svcCtx.Growth.GetHealth(l.ctx, uid, req.AccountId)
	if err != nil {
		return nil, err
	}
	return growthmap.Health(h), nil
}
