package checkups

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 GenerateCheckupNowLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *GenerateCheckupNowLogic) GenerateCheckupNow() (*types.WeeklyCheckupPublic, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	tz := ""
	if m, err := l.svcCtx.Members.FindByUID(l.ctx, uid); err == nil && m != nil {
		tz = m.Timezone
	}
	c, err := l.svcCtx.Growth.GenerateCheckup(l.ctx, uid, tz, true)
	if err != nil {
		return nil, err
	}
	return growthmap.Checkup(c), nil
}
