package forms

import (
	"errors"
	"fmt"
	"gadmin/internal/gorm/model"
)

type DeployNotifyReq struct {
	Code    int    `json:"code" form:"code"`
	Msg     string `json:"msg" form:"msg"`
	TraceID string `json:"traceID" form:"traceID"`
	LogFile string `json:"logFile" form:"logFile"`
}

func (req *DeployNotifyReq) Check() error {
	if req.TraceID == "" {
		return errors.New("TraceID不能为空")
	}
	return nil
}

type DeployLogViewReq struct {
	Id      int64  `json:"id" form:"id"`
	LogFile string `json:"log_file"` // 日志路径
}

func (req *DeployLogViewReq) Check() error {
	if req.Id <= 0 {
		return errors.New("操作记录ID不能为空")
	}

	return nil
}

type DeployLogViewModel struct {
	model.ServerDeployLog
	LogContent string `json:"stdout"`
}

type DeployLogListReq struct {
	ListReq
	DeployId int64 `json:"deployId" form:"deployId"`
}

func (req *DeployLogListReq) Check() error {
	if req.DeployId <= 0 {
		return errors.New("部署ID不能为空")
	}

	return nil
}

type DeployLogListModel struct {
	model.ServerDeployLog
	AdminName string `json:"adminName"`
}

type DeployListReq struct {
	ListReq
	GroupId       int64    `json:"groupId" form:"groupId"`
	ContainerName string   `json:"container_name" form:"container_name"`
	Name          string   `json:"name" form:"name"`
	ServerType    []string `json:"server_type[]" form:"server_type[]"`
	RunStatus     []string `json:"run_status[]" form:"run_status[]"`
	DeployStatus  []string `json:"deploy_status[]" form:"deploy_status[]"`
}

func (req *DeployListReq) Check() error {

	return nil
}

type DeployListData struct {
	model.ServerDeploy
	AdminName string `json:"adminName"`
}

type DeployTaskReq struct {
	ID         int64    `json:"id"  form:"id"`
	Version    string   `json:"version"  form:"version"`       // 部署版本号
	Type       int64    `json:"type"  form:"type"`             // 部署类型
	Ids        []int64  `json:"ids"  form:"ids"`               // 批量部署ID
	DeployServ []string `json:"deployServ"  form:"deployServ"` // 部署服务选项
}

func (req *DeployTaskReq) Check() error {
	switch req.Type {
	case 1:
		if req.ID <= 0 {
			return errors.New("部署ID不能为空")
		}
		req.Ids = []int64{req.ID}
	case 2:
		// 去重
		var ids []int64
		for _, id := range req.Ids {
			if id > 0 {
				ids = append(ids, id)
			}
		}
		req.Ids = ids

		if len(req.Ids) == 0 {
			return errors.New("请选择需要部署的服务器")
		}
	default:
		return errors.New(fmt.Sprintf("部署类型是无效的:%v", req.Type))
	}

	if req.Version == "" {
		return errors.New("版本号不能为空")
	}

	if len(req.DeployServ) == 0 {
		return errors.New("请选择部署服务")
	}
	return nil
}

type DeployStopReq struct {
	ID       int64   `json:"id"  form:"id"`
	Type     int64   `json:"type"  form:"type"` // 部署类型
	Ids      []int64 `json:"ids"  form:"ids"`
	StopType int64   `json:"stop_type"  form:"stop_type"` // 停服方式
}

func (req *DeployStopReq) Check() error {

	switch req.Type {
	case 1:
		if req.ID <= 0 {
			return errors.New("部署ID不能为空")
		}
		req.Ids = []int64{req.ID}
	case 2:
		// 去重
		var ids []int64
		for _, id := range req.Ids {
			if id > 0 {
				ids = append(ids, id)
			}
		}
		req.Ids = ids

		if len(req.Ids) == 0 {
			return errors.New("请选择需要部署的服务器")
		}
	default:
		return errors.New(fmt.Sprintf("部署类型是无效的:%v", req.Type))
	}

	if req.StopType != 2 && req.StopType != 9 {
		return errors.New("停服方式是无效的")
	}
	return nil
}

type DeployEditReq struct {
	ID            int64  `json:"id"  form:"id"`
	GroupID       int32  `json:"group_id"  form:"group_id"`             // 模板ID
	ContainerName string `json:"container_name"  form:"container_name"` // 加装类型
	Name          string `json:"name"  form:"name"`                     // 模板名称
	ServerType    string `json:"server_type"  form:"server_type"`
	DockerAddr    string `json:"docker_addr"  form:"docker_addr"` // 关联ID
	CaPem         string `json:"ca_pem"  form:"ca_pem"`           // 关联ID
	CertPem       string `json:"cert_pem"  form:"cert_pem"`       // 关联ID
	KeyPem        string `json:"key_pem"  form:"key_pem"`         // 关联ID
	Remark        string `json:"remark"  form:"remark"`           // 关联ID
	IsMproom      int32  `json:"is_mproom"  form:"is_mproom"`     // 是否启用IsMproom

}

type DeployDeleteReq struct {
	ID int64 `json:"id"  form:"id"`
}
