package routers

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

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

// CreateRequest struct to create router
type CreateRequest struct {
	// Access group ID
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Description
	// Required: true
	Description string `url:"description" json:"description" validate:"required"`

	// Name of acces group
	// Required: true
	DisplayName string `url:"display_name" json:"display_name" validate:"required"`

	// Enabled True or False
	// Required: true
	Enabled interface{} `url:"enabled" json:"enabled" validate:"required,isBool"`
}

// Create creates a access groups
func (i Routers) Create(ctx context.Context, req CreateRequest) (*RoutersModel, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/router/create"

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

	info := RoutersModel{}

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

	return &info, nil
}
