package core

import (
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/core"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
)

func ScheduleTaskByYamlHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.ScheduleTaskByYamlReq
		if err := httpx.Parse(r, &req); err != nil {
			result.HttpResult(r, w, nil, err)
			return
		}
		// 解析yaml文件
		_, fileHeader, err := r.FormFile("file")
		if err != nil {
			result.HttpResult(r, w, nil, err)
			return
		}
		err = utils.Yaml2struct(fileHeader, &req)
		if err != nil {
			result.HttpResult(r, w, nil, err)
			return
		}
		l := core.NewScheduleTaskByYamlLogic(r.Context(), svcCtx)
		resp, err := l.ScheduleTaskByYaml(&req)
		result.HttpResult(r, w, resp, err)
	}
}
