package adapters

import (
	"context"
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
	"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
	"gorm.io/gorm"
	"strconv"
	"time"
)

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

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

func (l *CreateAdapterLogic) CreateAdapter(req *types.AdapterCreateReq) (resp *types.AdapterResp, err error) {
	adapter := types.AdapterInfo{}
	existAdapter := types.AdapterResp{}
	resp = &types.AdapterResp{}
	utils.Convert(req, &adapter)
	//check name
	exist := l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&existAdapter).Error
	if !errors.Is(exist, gorm.ErrRecordNotFound) {
		resp = &existAdapter
		return resp, errors.New("name already exists")
	}
	//check type
	var arr = [...]string{"0", "1", "2"}
	found := false
	for _, str := range arr {
		if str == req.Type {
			found = true
			break
		}
	}
	if found == false {
		return nil, errors.New("type not found")
	}

	//check resourceTypeDict
	sql := `select t_dict_item.item_value
			from t_dict
					 join t_dict_item on t_dict.id = t_dict_item.dict_id
			where dict_code = 'adapter_type' and item_value = ?
			  and t_dict_item.parent_id != 0`
	err = l.svcCtx.DbEngin.Raw(sql, req.ResourceType).First(&types.DictItemInfo{}).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return nil, errors.New("resourceType error, please check!")
	}
	adapter.Id = utils.GenSnowflakeIDStr()
	adapter.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	result := l.svcCtx.DbEngin.Table("t_adapter").Create(&adapter)
	if result.Error != nil {
		return resp, result.Error
	}

	_ = l.svcCtx.DbEngin.Table("t_adapter").Where("name = ?", req.Name).First(&existAdapter).Error
	resp = &existAdapter
	Tadapter := models.TAdapterInfo{}
	if req.Type == "0" {
		if req.ResourceType == "01" {
			Tadapter.Id, _ = strconv.ParseInt(utils.GenSnowflakeIDStr(), 10, 64)
			Tadapter.AdapterId, _ = strconv.ParseInt(resp.Id, 10, 64)
			Tadapter.InfoName = "CloudInfoList"
			l.svcCtx.DbEngin.Table("t_adapter_info").Create(&Tadapter)
			resp.InfoName = "CloudInfoList"
		} else if req.ResourceType == "02" {
			Tadapter.Id, _ = strconv.ParseInt(utils.GenSnowflakeIDStr(), 10, 64)
			Tadapter.AdapterId, _ = strconv.ParseInt(resp.Id, 10, 64)
			Tadapter.InfoName = "VmInfoList"
			l.svcCtx.DbEngin.Table("t_adapter_info").Create(&Tadapter)
			resp.InfoName = "VmInfoList"
		}
	} else if req.Type == "1" {
		Tadapter.Id, _ = strconv.ParseInt(utils.GenSnowflakeIDStr(), 10, 64)
		Tadapter.AdapterId, _ = strconv.ParseInt(resp.Id, 10, 64)
		Tadapter.InfoName = "AiInfoList"
		l.svcCtx.DbEngin.Table("t_adapter_info").Create(&Tadapter)
		resp.InfoName = "AiInfoList"
	} else if req.Type == "2" {
		Tadapter.Id, _ = strconv.ParseInt(utils.GenSnowflakeIDStr(), 10, 64)
		Tadapter.AdapterId, _ = strconv.ParseInt(resp.Id, 10, 64)
		Tadapter.InfoName = "HpcInfoList"
		l.svcCtx.DbEngin.Table("t_adapter_info").Create(&Tadapter)
		resp.InfoName = "HpcInfoList"
	}
	return resp, nil
}
