package utilities

import (
	"context"
	"fmt"

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

func RGListComputesCheckPresence(ctx context.Context, plan *models.DataSourceRGListComputesModel, c *client.Client) (*rg.ListComputes, error) {
	listCompReq := rg.ListComputesRequest{
		RGID: uint64(plan.RGID.ValueInt64()),
	}

	if !plan.ComputeID.IsNull() {
		listCompReq.ComputeID = uint64(plan.ComputeID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		listCompReq.Name = plan.Name.ValueString()
	}
	if !plan.AccountID.IsNull() {
		listCompReq.AccountID = uint64(plan.AccountID.ValueInt64())
	}
	if !plan.TechStatus.IsNull() {
		listCompReq.TechStatus = plan.TechStatus.ValueString()
	}
	if !plan.Status.IsNull() {
		listCompReq.Status = plan.Status.ValueString()
	}
	if !plan.IPAddress.IsNull() {
		listCompReq.IPAddress = plan.IPAddress.ValueString()
	}
	if !plan.ExtNetName.IsNull() {
		listCompReq.ExtNetName = plan.ExtNetName.ValueString()
	}
	if !plan.ExtNetID.IsNull() {
		listCompReq.ExtNetID = uint64(plan.ExtNetID.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		listCompReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		listCompReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		listCompReq.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "RGListComputesCheckPresence: before call CloudBroker().RG().ListComputes", map[string]any{"response": listCompReq})
	rgListComp, err := c.CloudBroker().RG().ListComputes(ctx, listCompReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about resource group list computes with error: %w", err)
	}

	tflog.Info(ctx, "RGListComputesCheckPresence: response from CloudBroker().RG().ListComputes", map[string]any{"response": rgListComp})

	return rgListComp, err
}
