/*
Copyright (c) 2026 Huawei Technologies Co., Ltd.
openFuyao is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

package remote

import (
	ssusdk "atomgit.com/openeuler/ubs-engine.git/src/sdk/go/ssu"
)

// UBSE HTTP API paths.
const ubseSpacesPath = "/ubse/v1/ssu/spaces"

// LBA format constants.
const (
	lbaFormat512Bytes  = 512  // 512-byte LBA format
	lbaFormat4096Bytes = 4096 // 4096-byte (4K) LBA format
)

// allocRequest corresponds to UBSE POST /ubse/v1/ssu/spaces request body.
type allocRequest struct {
	Name      string `json:"name"`
	NsSize    uint64 `json:"nsSize"`
	NsNum     uint32 `json:"nsNum"`
	LbaFormat uint32 `json:"lbaFormat"`
	Strategy  uint8  `json:"strategy"`
	Tenant    string `json:"tenant"`
}

// allocNamespace corresponds to single namespace info in UBSE response.
type allocNamespace struct {
	TgtEid         string `json:"tgtEid"`
	TgtNqn         string `json:"tgtNqn"`
	DefaultHostNqn string `json:"defaultHostNqn"`
	NsUuid         string `json:"nsUuid"`
	NamespaceId    uint32 `json:"namespaceId"`
	NsDevPath      string `json:"nsDevPath"`
	NsSize         uint64 `json:"nsSize"`
	LbaFormat      uint32 `json:"lbaFormat"`
}

// allocResponseData corresponds to UBSE response data field.
type allocResponseData struct {
	Name          string           `json:"name"`
	Strategy      uint8            `json:"strategy"`
	NameSpaceList []allocNamespace `json:"nameSpaceList"`
}

func lbaFormatToBytes(format uint32) uint32 {
	if format == 1 {
		return lbaFormat4096Bytes
	}
	return lbaFormat512Bytes
}

// toAllocResult converts UBSE HTTP API response to ssusdk.UbsSsuAllocResult.
func toAllocResult(data allocResponseData) ssusdk.UbsSsuAllocResult {
	namespaces := make([]ssusdk.UbsSsuNamespaceInfo, 0, len(data.NameSpaceList))
	for _, ns := range data.NameSpaceList {
		namespaces = append(namespaces, ssusdk.UbsSsuNamespaceInfo{
			TgtEid:      ns.TgtEid,
			TgtNqn:      ns.TgtNqn,
			NsUuid:      ns.NsUuid,
			NamespaceId: ns.NamespaceId,
			NsDevPath:   ns.NsDevPath,
			NsSize:      ns.NsSize,
			LbaFormat:   ssusdk.UbsSsuLbaFormat(ns.LbaFormat),
		})
	}
	return ssusdk.UbsSsuAllocResult{
		Name:       data.Name,
		Strategy:   ssusdk.UbsSsuAllocStrategy(data.Strategy),
		Namespaces: namespaces,
	}
}