package member

import (
	"context"
	"strings"

	memberenum "gateway/internal/model/member/domain/enum"
	domusecase "gateway/internal/model/member/domain/usecase"
	"gateway/internal/svc"
)

func parseStepUpPurpose(raw string) (memberenum.StepUpPurpose, error) {
	p := memberenum.StepUpPurpose(strings.TrimSpace(raw))
	if p == "" {
		return memberenum.StepUpPurposeChangePassword, nil
	}
	if !p.Valid() {
		return "", errb.InputInvalidFormat("unsupported step-up purpose")
	}
	return p, nil
}

func requireStepUp(sc *svc.ServiceContext) error {
	if sc.MemberStepUp == nil {
		return errb.SysNotImplemented("member step-up not configured")
	}
	return nil
}

func issueStepUpAfterVerify(
	ctx context.Context,
	sc *svc.ServiceContext,
	tenantID, uid string,
	purpose memberenum.StepUpPurpose,
) (*domusecase.StepUpView, error) {
	if err := requireStepUp(sc); err != nil {
		return nil, err
	}
	return sc.MemberStepUp.Issue(ctx, &domusecase.IssueStepUpRequest{
		TenantID: tenantID,
		UID:      uid,
		Purpose:  purpose,
		TTL:      0,
	})
}
