package utilities

import (
	"context"
	"fmt"

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

func VFPoolListCheckPresence(ctx context.Context, plan *models.ListVFPoolModel, c *client.Client) (*vfpool.ListVFPool,
	error) {
	req := vfpool.ListRequest{}

	if !plan.ByID.IsNull() {
		req.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.GID.IsNull() {
		req.GID = uint64(plan.GID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		req.Name = plan.Name.ValueString()
	}
	if !plan.Description.IsNull() {
		req.Description = plan.Description.ValueString()
	}
	if !plan.Status.IsNull() {
		req.Status = plan.Status.ValueString()
	}
	if !plan.AccountAccess.IsNull() {
		req.AccountAccess = uint64(plan.AccountAccess.ValueInt64())
	}
	if !plan.RgAccess.IsNull() {
		req.RGAccess = uint64(plan.RgAccess.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		req.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		req.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		req.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "VFPoolListCheckPresence: before call CloudBroker().VFPool().List", map[string]any{"req": req})
	vfPool, err := c.CloudBroker().VFPool().List(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("VFPoolListCheckPresence: cannot get info about vfpool list")
	}

	tflog.Info(ctx, "VFPoolListCheckPresence: response from CloudBroker().VFPool().Get", map[string]any{"response": vfPool})

	return vfPool, err
}
