package secpolicies

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 security policy
type CreateRequest struct {
	// Access group ID
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Applied to net object group ID
	// Required: true
	AppliedToNetObjectGroupID string `url:"applied_to_net_object_group_id" json:"applied_to_net_object_group_id" validate:"required"`

	// Description of the schedule rule
	// Required: true
	Description string `url:"description" json:"description"`

	// Display name of the schedule rule
	// Required: true
	DisplayName string `url:"display_name" json:"display_name"`

	// Enabled status of the schedule rule
	// Required: true
	Enabled bool `url:"enabled" json:"enabled"`

	// End date and time for the schedule rule
	// Required: false
	EndDateTime string `url:"end_date_time,omitempty" json:"end_date_time,omitempty"`

	// Insert up reference
	// Required: false
	InsertUp string `url:"insert_up,omitempty" json:"insert_up,omitempty"`

	// Locked at timestamp
	// Required: false
	LockedAt string `url:"locked_at,omitempty" json:"locked_at,omitempty"`

	// Schedule cron expression
	// Required: false
	ScheduleCron string `url:"schedule_cron,omitempty" json:"schedule_cron,omitempty"`

	// Start date and time for the schedule rule
	// Required: false
	StartDateTime string `url:"start_date_time,omitempty" json:"start_date_time,omitempty"`
}

// Create creates a security policy
func (i SecurityPolicies) Create(ctx context.Context, req CreateRequest) (*SecurityPolicySummary, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/security_policy/create"

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

	info := SecurityPolicySummary{}

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

	return &info, nil
}
