package controllers

import (
	"intent-system/pkg/privilege"
	"intent-system/pkg/proto"
	"intent-system/pkg/sessions"

	"github.com/civet148/log"
	"github.com/gin-gonic/gin"
)

func (m *Controller) DeployStatus(c *gin.Context) {

	log.Infof(".......controller.................DeployStatus.......................\n\n")

	var req proto.DeployStatusReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}

	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.Null)
	}
	if !ok {
	}

	resp, code := m.DeployCore.Status(&req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}

	m.OK(c, resp, 0, 1)

}

func (m *Controller) DeployDeploy(c *gin.Context) {

	log.Infof(".......controller.................Deploy.......................\n\n")

	var req proto.DeployDeployReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}

	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.Null)
	}
	if !ok {
	}

	resp, code := m.DeployCore.Deploy(&req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}

	m.OK(c, resp, 0, 1)

}

func (m *Controller) DeployDelete(c *gin.Context) {

	log.Infof(".......controller.................Deploy....Delete...................\n\n")

	var req proto.DeployDeleteReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}

	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.Null)
	}
	if !ok {
	}

	resp, code := m.DeployCore.Delete(&req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}

	m.OK(c, resp, 0, 1)

}

func (m *Controller) DeployStart(c *gin.Context) {

	log.Infof(".......controller.................Deploy....Start...................\n\n")

	var req proto.DeployStartReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}

	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.Null)
	}
	if !ok {
	}

	resp, code := m.DeployCore.Start(&req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}

	m.OK(c, resp, 0, 1)

}

func (m *Controller) DeployStop(c *gin.Context) {

	log.Infof(".......controller.................Deploy....Stop...................\n\n")

	var req proto.DeployStopReq
	if err := m.bindJSON(c, &req); err != nil {
		log.Errorf("%s", err)
		return
	}

	ctx := sessions.GetContext(c)
	var ok bool
	if ctx != nil {
		ok = m.CheckPrivilege(c, ctx, privilege.Null)
	}
	if !ok {
	}

	resp, code := m.DeployCore.Stop(&req)
	if !code.Ok() {
		m.Error(c, code)
		return
	}

	m.OK(c, resp, 0, 1)

}
