package api

import (
	"easyvqd/internal/core/vqd"
	"fmt"
	"git.lnton.com/lnton/pkg/reason"
	"github.com/gin-gonic/gin"
	"strconv"
)

func (a VqdTaskAPI) findVqdTimeTemplate(c *gin.Context, in *vqd.FindVqdTimeTemplateInput) (any, error) {
	items, total, err := a.core.FindVqdTimeTemplate(c.Request.Context(), in)
	rows := make([]map[string]interface{}, 0)

	for _, item := range items {
		//row := structs.Map(item)
		row := make(map[string]interface{})
		row["id"] = item.ID
		row["name"] = item.Name
		row["des"] = item.Des
		row["plans"] = item.Plans
		row["enable"] = item.Enable
		row["is_default"] = item.IsDefault
		row["created_at"] = item.CreatedAt
		row["updated_at"] = item.UpdatedAt

		rows = append(rows, row)
	}

	return gin.H{"items": rows, "total": total}, err
}
func (a VqdTaskAPI) getVqdTimeTemplate(c *gin.Context, _ *struct{}) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	item, err := a.core.GetVqdTimeTemplate(c.Request.Context(), ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`find vqdcms [%d] err [%s]`, ID, err.Error()))
	}
	row := make(map[string]interface{})

	row["id"] = item.ID
	row["name"] = item.Name
	row["is_default"] = item.IsDefault
	row["des"] = item.Des
	row["plans"] = item.Plans
	row["enable"] = item.Enable
	row["created_at"] = item.CreatedAt
	row["updated_at"] = item.UpdatedAt

	return gin.H{"data": row}, err
}
func (a VqdTaskAPI) addVqdTimeTemplate(c *gin.Context, in *vqd.AddVqdTimeTemplateInput) (any, error) {
	plan, err := a.core.AddVqdTimeTemplate(c.Request.Context(), in)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`add vqdcms err [%s]`, err.Error()))
	}

	out, _, err := a.core.FindVqdPlanID(c.Request.Context(), plan.ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`find plan all err [%s]`, err.Error()))
	}
	for _, task := range out {
		err = a.vqdSdkCore.UpdateTaskVqd(task.ID)
		if err != nil {
			continue
		}
	}
	return gin.H{"data": "OK!"}, err
}

func (a VqdTaskAPI) editVqdTimeTemplate(c *gin.Context, in *vqd.EditVqdTimeTemplateInput) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	plan, err := a.core.GetVqdTimeTemplate(c.Request.Context(), ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`find vqdcms [%d] err [%s]`, ID, err.Error()))
	}

	_, err = a.core.EditVqdTimeTemplate(c.Request.Context(), in, ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`edit vqdcms err [%s]`, err.Error()))
	}
	out, _, err := a.core.FindVqdPlanID(c.Request.Context(), plan.ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`find plan all err [%s]`, err.Error()))
	}
	for _, task := range out {
		err = a.vqdSdkCore.UpdateTaskVqd(task.ID)
		if err != nil {
			continue
		}
	}
	return gin.H{"data": "OK!"}, err
}

func (a VqdTaskAPI) delVqdTimeTemplate(c *gin.Context, _ *struct{}) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	info, err := a.core.GetVqdTimeTemplate(c.Request.Context(), ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`find vqdcms [%d] err [%s]`, ID, err.Error()))
	}
	if info.IsDefault {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`计划模板不支持删除 [%s] `, info.Name))
	}
	planInfo, err := a.core.FindVqdTaskPlanID(c.Request.Context(), ID)
	if err == nil {
		if planInfo != nil {
			return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`计划关联【%s】任务,需先清理【%s】任务后才能删除模板！`, planInfo.Name, planInfo.Name))
		}
	}
	_, err = a.core.DelVqdTimeTemplate(c.Request.Context(), ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`del vqdcms [%d] err [%s]`, ID, err.Error()))
	}

	return gin.H{"data": "OK!"}, err
}
