package qospolicy

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

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

type UpdateRequest struct {
	// QoS policy ID
	// Required: true
	QoSPolicyID uint64 `url:"qos_policy_id" json:"qos_policy_id" validate:"required"`

	// New QoS policy name
	// Required: false
	Name *string `url:"name,omitempty" json:"name,omitempty"`

	// New QoS policy description
	// Required: false
	Description *string `url:"description,omitempty" json:"description,omitempty"`
}

func (qp QoSPolicy) Update(ctx context.Context, req UpdateRequest) (*RecordQoSPolicy, error) {
	res, err := qp.UpdateRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := RecordQoSPolicy{}

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

	return &info, nil
}

func (qp QoSPolicy) UpdateRaw(ctx context.Context, req UpdateRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/network_qos_policy/update"

	res, err := qp.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
	return res, err
}
