package cluster

import (
	"fmt"

	"github.com/gin-gonic/gin"

	"github.com/tidbcloud/devops/api"
	k8supgradeapi "github.com/tidbcloud/devops/api/k8supgrade"
	ctrl "github.com/tidbcloud/devops/pkg/controller"
	"github.com/tidbcloud/devops/pkg/service/k8supgrade/infrainfo"
)

// ListK8SInformation
//
//	@Id				ListK8SInformation
//	@Description	list k8s infra information
//	@Tags			ClusterService
//	@Accept			json
//	@Produce		json
//	@Param			req	query		k8supgradeapi.ListK8SInformationReq	true	"ListK8SInformationReq"
//	@Success		200	{object}	k8supgradeapi.ListK8SInformationResp
//	@Failure		500	{object}	api.BaseResp
//	@Router			/api/v1/devops/cluster/k8sInfos [get]
func (c *ClusterController) ListK8SInformation(ctx *gin.Context) {
	req := &k8supgradeapi.ListK8SInformationReq{}
	if err := ctx.ShouldBindQuery(req); err != nil {
		ctrl.FailWithCode(ctx, err, api.ErrCodeInvalidArgument)
		return
	}

	if len(req.CloudProvider) == 0 || len(req.Version) == 0 {
		ctrl.FailWithCode(ctx, fmt.Errorf("must provide at least one cloudProvider and version"), api.ErrCodeInvalidArgument)
		return
	}

	list, err := c.InfraInfoSvc.ListK8SInformation(ctx, &infrainfo.ListK8SInformationReq{
		CloudProvider: req.CloudProvider,
		Version:       req.Version,
	})
	if err != nil {
		ctrl.FailWithCode(ctx, err, api.ErrCodeInternal)
		return
	}
	resp := &k8supgradeapi.ListK8SInformationResp{K8sInfoList: list}
	ctrl.Success(ctx, resp)
}

// GetK8SInformation
//
//	@Id				GetK8SInformation
//	@Description	get specific k8s infra information
//	@Tags			ClusterService
//	@Accept			json
//	@Produce		json
//	@Param			cloud_provider	path		string	true	"cloud provider"
//	@Param			region			path		string	true	"region"
//	@Param			k8s_name		path		string	true	"k8s name"
//	@Success		200				{object}	k8supgradeapi.GetK8SInformationResp
//	@Failure		500				{object}	api.BaseResp
//	@Router			/api/v1/devops/cluster/k8sInfos/{cloud_provider}/{region}/{k8s_name} [get]
func (c *ClusterController) GetK8SInformation(ctx *gin.Context) {
	info, err := c.InfraInfoSvc.GetK8sInformation(ctx, &infrainfo.GetK8SInformationReq{
		CloudProvider: ctx.Param("cloud_provider"),
		Region:        ctx.Param("region"),
		Name:          ctx.Param("k8s_name"),
	})
	if err != nil {
		ctrl.FailWithCode(ctx, err, api.ErrCodeInternal)
		return
	}
	resp := &k8supgradeapi.GetK8SInformationResp{K8sInfo: *info}
	ctrl.Success(ctx, resp)
}
