package logic

import (
	"context"
	"net/http"
	"strings"

	"dag-jupyter/internal/api"
	"dag-jupyter/internal/svc"
	"dag-jupyter/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type StartOpLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewStartOpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StartOpLogic {
	return &StartOpLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func formatPassword(password string) string {
	password = strings.ReplaceAll(password, "$", `\$`)
	password = strings.ReplaceAll(password, ",", `\,`)
	return password
}

// helm upgrade --install aihub-dag-jpt-1  ./chart -f chart/values.yaml -n airflow \
// --set "ingress.jupyterlab.hosts[0].host=jupyterlab.sxkj.com,ingress.jupyterlab.hosts[0].paths[0].path=/nbsss" \
// --set jupyterlab.image.repository=SXKJ:32775/jupyterlab \
// --set jupyterlab.image.tag=dev
// --set jupyterlab.config.password="\"argon2:\$argon2id\$v=19\$m=10240\,t=10\,p=8\$vSn0+TR0clV8kla6yqcZqQ\$XgjDauBkBL5aY8C8668HKJjoTX7BUT/WccJ+BeANHq4\"" \
// --set jupyterlab.config.baseUrl="/nbsss" \
// --set jupyterlab.config.workspace="hello2" \
// --kubeconfig=`pwd`/config-sxkj.yaml

func (l *StartOpLogic) StartOp(req *types.CreateJupyterInfo) (resp *types.Response, err error) {
	req.Password = formatPassword(req.Password)
	err = api.Install(req)
	data := map[string]interface{}{
		"namespace":    req.Namespace,
		"release_name": req.ReleaseName,
		"base_url":     req.BaseUrl,
		"image":        req.Image,
		"host":         req.Host,
		"tag":          req.Tag,
		"path":         req.Path,
		"workspace":    req.Workspace,
	}
	if err != nil {
		return &types.Response{
			Data: err.Error(),
			Code: http.StatusBadRequest,
		}, nil
	}

	return &types.Response{
		Data: data,
		Code: http.StatusOK,
	}, nil

}
