package api

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

func (a VqdTaskAPI) findVqdAlarm(c *gin.Context, in *vqd.FindVqdAlarmInput) (any, error) {
	items, total, err := a.core.FindVqdAlarm(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["is_deep"] = item.IsDeep
		row["channel_id"] = item.ChannelID
		row["channel_name"] = item.ChannelName
		row["plan_id"] = item.PlanID
		row["plan_name"] = item.PlanName
		row["task_template_id"] = item.TaskTemplateID
		row["task_template_name"] = item.TaskTemplateName
		row["task_id"] = item.TaskID
		row["task_name"] = item.TaskName
		row["file_path"] = item.FilePath
		row["abnormals"] = item.Abnormals
		row["default_values"] = item.DefaultValues
		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) getVqdAlarm(c *gin.Context, _ *struct{}) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	item, err := a.core.GetVqdAlarm(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["is_deep"] = item.IsDeep
	row["channel_id"] = item.ChannelID
	row["channel_name"] = item.ChannelName
	row["plan_id"] = item.PlanID
	row["plan_name"] = item.PlanName
	row["task_template_id"] = item.TaskTemplateID
	row["task_template_name"] = item.TaskTemplateName
	row["task_id"] = item.TaskID
	row["task_name"] = item.TaskName
	row["file_path"] = item.FilePath
	row["abnormals"] = item.Abnormals
	row["default_values"] = item.DefaultValues
	row["created_at"] = item.CreatedAt
	row["updated_at"] = item.UpdatedAt

	return gin.H{"data": row}, err
}

func (a VqdTaskAPI) delVqdAlarm(c *gin.Context, _ *struct{}) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	_, err := a.core.DelVqdAlarm(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
}
func (a VqdTaskAPI) delVqdAlarmAll(c *gin.Context, in *vqd.DelVqdAlarmInput) (any, error) {
	if len(in.IDs) == 0 {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`del vqdcms ids is empty`))
	}
	_, err := a.core.DelVqdAlarmAll(c.Request.Context(), in.IDs)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`del vqdcms [%d] err [%s]`, in.IDs, err.Error()))
	}
	return gin.H{"data": "OK!"}, err
}
