package inference

import (
	"context"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/schedulers/option"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/inference"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/inference/textInference"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
	"strconv"
)

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

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

func (l *TextToTextInferenceLogic) TextToTextInference(req *types.TextToTextInferenceReq) (resp *types.TextToTextInferenceResp, err error) {
	resp = &types.TextToTextInferenceResp{}

	instance, err := l.svcCtx.Scheduler.AiStorages.GetInferDeployInstanceById(req.InstanceId)
	if err != nil {
		return nil, err
	}
	if instance == nil {
		return nil, errors.New("instance is empty ")
	}

	adapterId := strconv.FormatInt(instance.AdapterId, 10)

	opt := &option.InferOption{
		TaskName:  req.TaskName,
		TaskDesc:  req.TaskDesc,
		ModelType: req.ModelType,
		AdapterId: adapterId,
	}

	adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId)

	infer, err := textInference.NewTextToText(opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, instance)
	if err != nil {
		return nil, err
	}
	textInfer, err := textInference.New(infer, opt, l.svcCtx.Scheduler.AiStorages, adapterName)
	if err != nil {
		return nil, err
	}

	in := inference.Inference{
		In: textInfer,
	}

	id, err := in.In.CreateTask()
	if err != nil {
		return nil, err
	}
	err = in.In.InferTask(id)
	if err != nil {
		return nil, err
	}

	return resp, nil
}
