package stpolicy

import (
	"context"
	"strconv"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/stpolicy"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func utilityStoragePolicyCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*stpolicy.InfoStoragePolicy, error) {
	c := m.(*controller.ControllerCfg)
	req := stpolicy.GetRequest{}

	if d.Id() != "" {
		storagePolicyID, _ := strconv.ParseUint(d.Id(), 10, 64)
		req.StoragePolicyID = storagePolicyID
	} else {
		req.StoragePolicyID = uint64(d.Get("storage_policy_id").(int))
	}

	storagePolicyData, err := c.CloudAPI().StPolicy().Get(ctx, req)
	if err != nil {
		return nil, err
	}

	return storagePolicyData, nil
}
