package logicalports

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 LogicalPortsForExcludeFromFirewall struct {
	// Exclude IP Addresses
	// Required: true
	ExcludeIPAddresses bool `url:"exclude_ip_addresses" json:"exclude_ip_addresses" validate:"required"`

	// ID
	// Required: true
	ID string `url:"id" json:"id" validate:"required"`

	// Version ID
	// Required: true
	VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
}

// ExcludeFirewallRequest struct to exclude firewall for logical port
type ExcludeFirewallRequest struct {
	// Access Group ID
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Logical Ports For Exclude From Firewall
	// Required: true
	LogicalPortsForExcludeFromFirewall []LogicalPortsForExcludeFromFirewall `json:"logical_ports_for_exclude_from_firewall" validate:"required,dive"`

	// Exclusion Reason
	// Required: false
	ExclusionReason string `url:"exclusion_reason,omitempty" json:"exclusion_reason,omitempty"`
}

// ExcludeFirewall excludes firewall from a logical port
func (lp LogicalPorts) ExcludeFirewall(ctx context.Context, req ExcludeFirewallRequest) (*RecordVersion, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/logical_port/exclude_firewall"

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

	info := RecordVersion{}

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

	return &info, nil
}
