package secgroup

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

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

type CreateRequest struct {
	// Account ID that owns security group
	// Required: true
	AccountID uint64 `url:"account_id" json:"account_id" validate:"required"`

	// Security group name
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`

	// Security group description
	// Required: false
	Description string `url:"description,omitempty" json:"description,omitempty"`
}

func (sg SecurityGroup) Create(ctx context.Context, req CreateRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/security_group/create"

	res, err := sg.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

}
