package stpolicy

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

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

type AddPoolRequest struct {
	// ID of storage policy
	// Required: true
	StoragePolicyID uint64 `url:"storage_policy_id" json:"storage_policy_id" validate:"required"`

	// Storage endpoint provider ID to add
	// Required: true
	SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"`

	// Pool to add
	// Required: true
	PoolName string `url:"pool_name" json:"pool_name" validate:"required"`
}

func (sp StPolicy) AddPool(ctx context.Context, req AddPoolRequest) (*InfoStoragePolicyWithID, error) {
	res, err := sp.AddPoolRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := InfoStoragePolicyWithID{}

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

	return &info, nil
}

func (sp StPolicy) AddPoolRaw(ctx context.Context, req AddPoolRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/storage_policy/add_pool"

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