package secgroup

import (
	"context"
	"encoding/json"
	"net/http"

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

type GetRequest struct {
	// ID of security group
	// Required: true
	SecurityGroupID uint64 `url:"security_group_id" json:"security_group_id" validate:"required"`
}

func (sg SecurityGroup) Get(ctx context.Context, req GetRequest) (*RecordSecurityGroup, error) {
	res, err := sg.GetRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := RecordSecurityGroup{}

	err = json.Unmarshal(res, &info)
	if err != nil {
		return nil, err
	}

	return &info, nil
}

func (sg SecurityGroup) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/security_group/get"

	res, err := sg.client.DecortApiCall(ctx, http.MethodGet, url, req)
	return res, err
}
