package rg

import (
	"context"
	"net/http"
	"strconv"

	"repository.basistech.ru/BASIS/dynamix-golang-sdk/v14/internal/constants"
	"repository.basistech.ru/BASIS/dynamix-golang-sdk/v14/internal/validators"
)

// CreateRequest struct to create resource group
type CreateRequest struct {
	// Account, which will own this resource group
	// Required: true
	AccountID uint64 `url:"accountId" json:"accountId" validate:"required"`

	// Grid ID
	// Required: true
	GID uint64 `url:"gid" json:"gid" validate:"required"`

	// Name of this resource group. Must be unique within the account
	// Required: true
	Name string `url:"name" json:"name" validate:"required,min=2"`

	// Storage policies
	// Required: false
	StoragePolicies []StoragePolicy `url:"storage_policies" json:"storage_policies"`

	// Max size of memory in MB
	// Required: false
	MaxMemoryCapacity int64 `url:"maxMemoryCapacity,omitempty" json:"maxMemoryCapacity,omitempty"`

	// Max size of aggregated virtual disks in GB
	// Required: false
	MaxVDiskCapacity int64 `url:"maxVDiskCapacity,omitempty" json:"maxVDiskCapacity,omitempty"`

	// Max number of CPU cores
	// Required: false
	MaxCPUCapacity int64 `url:"maxCPUCapacity,omitempty" json:"maxCPUCapacity,omitempty"`

	// Max number of assigned public IPs
	// Required: false
	MaxNumPublicIP int64 `url:"maxNumPublicIP,omitempty" json:"maxNumPublicIP,omitempty"`

	// Username - owner of this resource group.
	// Leave blank to set current user as owner
	// Required: false
	Owner string `url:"owner,omitempty" json:"owner,omitempty"`

	// Type of the default network for this resource group.
	// virtual machines created in this resource group will be by default connected to this network.
	// Allowed values are:
	//	- PRIVATE
	//	- PUBLIC
	//	- NONE
	// Required: false
	DefNet string `url:"def_net,omitempty" json:"def_net,omitempty" validate:"omitempty,rgDefNet"`

	// Private network IP CIDR if default network PRIVATE
	// Required: false
	IPCIDR string `url:"ipcidr,omitempty" json:"ipcidr,omitempty"`

	// Text description of this resource group
	// Required: false
	Description string `url:"desc,omitempty" json:"desc,omitempty"`

	// External network ID
	// Required: false
	ExtNetID uint64 `url:"extNetId,omitempty" json:"extNetId,omitempty"`

	// External IP address
	// Required: false
	ExtIP string `url:"extIp,omitempty" json:"extIp,omitempty"`

	// SDN access group id
	// Required: false
	SDNAccessGroupID string `url:"sdn_access_group_id,omitempty" json:"sdn_access_group_id,omitempty"`
}

type StoragePolicy struct {
	ID    uint64 `url:"id" json:"id"`
	Limit int    `url:"limit" json:"limit"`
}

// Create creates resource group
func (r RG) Create(ctx context.Context, req CreateRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/rg/create"

	res, err := r.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
	if err != nil {
		return 0, err
	}

	result, err := strconv.ParseUint(string(res), 10, 64)
	if err != nil {
		return 0, err
	}

	return result, nil
}
