package defsecpolicies

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

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

// UpdateRequest struct to update default security policy
type UpdateRequest struct {
	// ID of the access group
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// ID of the version
	// Required: true
	VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`

	// Default ACL drop behavior
	// Required: false
	DefaultACLDrop string `url:"default_acl_drop,omitempty" json:"default_acl_drop,omitempty"`

	// Default open session drop flag
	// Required: false
	DefaultOpenSessionDrop bool `url:"default_open_session_drop,omitempty" json:"default_open_session_drop,omitempty"`
}

// Update updates a default security policy
func (i DefaultSecurityPolicies) Update(ctx context.Context, req UpdateRequest) (*SecurityPolicy, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/default_security_policy/update"

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

	info := SecurityPolicy{}

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

	return &info, nil
}
