package qospolicy

import (
	"context"
	"net/http"
	"strconv"

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

type CreateRuleRequest struct {
	// ID of the parent QoS policy this rule belongs to
	// Required: true
	QoSPolicyID uint64 `url:"qos_policy_id" json:"qos_policy_id" validate:"required"`

	// QoS policy rule description
	// Default: ""
	// Required: false
	Description string `url:"description,omitempty" json:"description,omitempty"`

	// Rule priority in OVS (higher number = higher priority)
	// Possible values: [0, 65535]
	// Required: true
	Priority *uint64 `url:"priority" json:"priority" validate:"required,min=0,max=65535"`

	// Traffic direction
	// Required: true
	Direction string `url:"direction" json:"direction" validate:"required,oneof=outbound"`

	// IP protocol to match
	// Possible values: tcp, udp, icmp
	// Required: true
	Protocol string `url:"protocol" json:"protocol" validate:"required,oneof=tcp udp icmp"`

	// Priority Code Point. L2 marking
	// Possible values: [0, 7]
	// Required: false
	PCPTag *uint64 `url:"pcp_tag,omitempty" json:"pcp_tag,omitempty" validate:"required_without_all=DSCPValue,omitempty,min=0,max=7"`

	// DSCP value (0-63). L3 marking
	// Possible values: [0, 63]
	// Required: false
	DSCPValue *uint64 `url:"dscp_value,omitempty" json:"dscp_value,omitempty" validate:"required_without_all=PCPTag,omitempty,min=0,max=63"`

	// Source IP address for traffic filtering
	// Required: false
	SrcIP string `url:"src_ip,omitempty" json:"src_ip,omitempty"`

	// Destination IP address for traffic filtering
	// Required: false
	DstIP string `url:"dst_ip,omitempty" json:"dst_ip,omitempty"`

	// Source MAC address for traffic filtering
	// Required: false
	SrcMAC string `url:"src_mac,omitempty" json:"src_mac,omitempty" validate:"omitempty,mac"`

	// Destination MAC address for traffic filtering
	// Required: false
	DstMAC string `url:"dst_mac,omitempty" json:"dst_mac,omitempty" validate:"omitempty,mac"`

	// Source port for traffic filtering (TCP/UDP only)
	// Possible values: [1, 65535]
	// Required: false
	SrcPort uint64 `url:"src_port,omitempty" json:"src_port,omitempty" validate:"omitempty,min=1,max=65535"`

	// Destination port for traffic filtering (TCP/UDP only)
	// Possible values: [1, 65535]
	// Required: false
	DstPort uint64 `url:"dst_port,omitempty" json:"dst_port,omitempty" validate:"omitempty,min=1,max=65535"`
}

// CreateRule creates a new rule for a QoS policy
func (qp QoSPolicy) CreateRule(ctx context.Context, req CreateRuleRequest) (uint64, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return 0, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/network_qos_policy/create_rule"

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

	result, err := strconv.ParseUint(string(res), 10, 64)
	if err != nil {
		return 0, err
	}

	return result, nil
}
