package utilities

import (
	"context"
	"fmt"

	"github.com/hashicorp/terraform-plugin-log/tflog"
	decort "repository.basistech.ru/BASIS/decort-golang-sdk"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/rg"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/rg/models"
)

func RGUsageCheckPresence(ctx context.Context, plan *models.DataSourceRGUsageModel, c *decort.DecortClient) (*rg.RecordResourceUsage, error) {
	usageReq := rg.UsageRequest{RGID: uint64(plan.RGID.ValueInt64())}

	if !plan.Reason.IsNull() {
		usageReq.Reason = plan.Reason.ValueString()
	}

	tflog.Info(ctx, "RGUsageCheckPresence: before call CloudAPI().RG().Usage", map[string]any{"response": usageReq})
	usage, err := c.CloudAPI().RG().Usage(ctx, usageReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about resource group resource usage with error: %w", err)
	}

	tflog.Info(ctx, "RGUsageCheckPresence: response from CloudAPI().RG().Usage", map[string]any{"response": usage})

	return usage, err
}
