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

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

func (l *ListCheckupsLogic) ListCheckups(req *types.ListCheckupsReq) (*types.CheckupListData, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	list, total, err := l.svcCtx.Growth.ListCheckups(l.ctx, uid, req.Page, req.PageSize)
	if err != nil {
		return nil, err
	}
	return &types.CheckupListData{
		List: growthmap.CheckupList(list),
		Pagination: growthmap.Pagination(req.Page, req.PageSize, total),
	}, nil
}
