package rule

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

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

// GetRequest struct to get a security rules
type GetRequest struct {
	// Security policy ID
	// Required: true
	SecurityPolicyID string `url:"security_policy_id" json:"security_policy_id" validate:"required"`

	// Security rule ID
	// Required: true
	SecurityRuleID string `url:"security_rule_id" json:"security_rule_id" validate:"required"`
}

// Get a security policies
func (i Rule) Get(ctx context.Context, req GetRequest) (*SecurityRule, error) {
	res, err := i.GetRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	result := SecurityRule{}

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

	return &result, nil
}

// GetRaw gets a security rule as an array of bytes
func (a Rule) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {

	if err := validators.ValidateRequest(req); err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/security_policy/rule/get"

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