package central

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	centralapi "github.com/tidbcloud/core-svc/idl/pbgen/proto/central"

	"github.com/tidbcloud/tidb-management-service/tempo-wrapper/internal/api/types"
	"github.com/tidbcloud/tidb-management-service/tempo-wrapper/internal/api/utils"
)

func GetThreeArch(ctx *gin.Context) {
	req := &centralapi.GetThreeArchReq{}
	err := types.ShouldBindJSON(ctx, req)
	if err != nil {
		types.BadRequestResp(ctx, fmt.Errorf("bind request body failed: %w", err))
		return
	}

	// check org/project id
	orgId, err := strconv.ParseUint(ctx.Param("org_id"), 10, 64)
	if err != nil {
		types.UnauthorizedResp(ctx, err)
		return
	}
	projectId, err := strconv.ParseUint(ctx.Param("project_id"), 10, 64)
	if err != nil {
		types.UnauthorizedResp(ctx, err)
		return
	}

	// set org and project id
	req.OrgId = orgId
	req.ProjectId = projectId

	ok, err := utils.IsDedicatedAllow(ctx.Request.Context(), orgId, projectId)
	if err != nil {
		types.InternalErrorResp(ctx, fmt.Errorf("check is dedicated cluster failed: %w", err))
		return
	}

	types.SuccessResp(ctx, &centralapi.GetThreeArchResp{ThreeArch: ok})
}
