package segments

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

	"repository.basistech.ru/BASIS/dynamix-golang-sdk/v13/internal/validators"
)

// GetStatusRequest struct to get information about segment status
type GetStatusRequest struct {
	// ID of segment
	// Required: true
	SegmentID string `url:"segment_id" json:"segment_id" validate:"required"`

	// ID of version
	// Required: false
	VersionID uint64 `url:"version_id,omitempty" json:"version_id,omitempty"`

	// Get detailed status or not
	// Required: false
	Detailed interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`
}

// GetStatus gets segment status
func (s Segments) GetStatus(ctx context.Context, req GetStatusRequest) (string, error) {
	type temp struct {
		Status string `json:"status"`
	}

	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/segment/get_status"

	res, err := s.client.DecortApiCall(ctx, http.MethodGet, url, req)
	if err != nil {
		return "", err
	}

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

	return info.Status, nil
}
