package segments

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"
)

// CreateRequest struct for creating segment
type CreateRequest struct {
	// Identifier of the parent access group
	// Required: false
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Detailed description of the segment
	// Required: true
	Description string `url:"description" json:"description" validate:"required"`

	// User-friendly name for the segment
	// Required: true
	DisplayName string `url:"display_name" json:"display_name" validate:"required"`

	// Whether the network is enabled
	// Required: true
	Enabled bool `url:"enabled" json:"enabled"`

	// IPv4 subnet in CIDR notation (Either subnet_v4 or subnet_v6 must be specified)
	// Required: false
	SubnetV4 string `url:"subnet_v4,omitempty" json:"subnet_v4,omitempty"`

	// IPv6 subnet in CIDR notation (Either subnet_v4 or subnet_v6 must be specified)
	// Required: false
	SubnetV6 string `url:"subnet_v6,omitempty" json:"subnet_v6,omitempty"`

	// DHCP IPv4
	// Required: false
	DHCPv4 *DHCPv4ConfigRequest `url:"-" json:"dhcp_v4,omitempty"`

	// DHCP IPv6
	// Required: false
	DHCPv6 *DHCPv6ConfigRequest `url:"-" json:"dhcp_v6,omitempty"`

	// Segment type
	// Required: false
	Type string `url:"type,omitempty" json:"type,omitempty"`
}

type DHCPv4ConfigRequest struct {
	// DNS
	// Required: false
	DNS []string `url:"dns,omitempty" json:"dns,omitempty"`

	// Excluded address ranges
	// Required: false
	ExcludedAddressRanges []string `url:"excluded_address_ranges,omitempty" json:"excluded_address_ranges,omitempty"`

	// Gateway
	// Required: true
	Gateway string `url:"gateway" json:"gateway" validate:"required"`

	// Lease time
	// Required: false
	LeaseTime uint64 `url:"lease_time,omitempty" json:"lease_time,omitempty"`

	// Server IP
	// Required: true
	ServerIP string `url:"server_ip" json:"server_ip" validate:"required"`

	// Server MAC
	// Required: false
	ServerMAC string `url:"server_mac,omitempty" json:"server_mac,omitempty"`

	// Whether the config is enabled
	// Required: true
	Enabled bool `url:"enabled" json:"enabled"`
}

type DHCPv6ConfigRequest struct {
	// Address prefix
	// Required: true
	AddressPrefix string `url:"address_prefix" json:"address_prefix" validate:"required"`

	// DNS
	// Required: false
	DNS []string `url:"dns,omitempty" json:"dns,omitempty"`

	// Lease time
	// Required: true
	LeaseTime uint64 `url:"lease_time,omitempty" json:"lease_time,omitempty"`

	// Server MAC
	// Required: true
	ServerMAC string `url:"server_mac,omitempty" json:"server_mac,omitempty"`

	// Whether the config is enabled
	// Required: true
	Enabled bool `url:"enabled" json:"enabled"`
}

// Create creates segment
func (s Segments) Create(ctx context.Context, req CreateRequest) (*SegmentResponse, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/segment/create"

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

	info := SegmentResponse{}

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

	return &info, nil
}
