package grid

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

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

// GetSettingsRequest struct to get grid settings
type GetSettingsRequest struct {
	// Grid (platform) ID
	// Required: true
	GID uint64 `url:"grid_id" json:"grid_id" validate:"required"`
}

// GetSettings gets settings grid by ID as a RecordSettingsGrid struct
func (g Grid) GetSettings(ctx context.Context, req GetSettingsRequest) (*RecordSettingsGrid, error) {
	res, err := g.GetSettingsRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	settings := RecordSettingsGrid{}

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

	return &settings, nil
}

// GetSettingsRaw gets settings grid by ID as an array of bytes
func (g Grid) GetSettingsRaw(ctx context.Context, req GetSettingsRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/grid/getSettings"

	res, err := g.client.DecortApiCall(ctx, http.MethodPost, url, req)
	return res, err
}
