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 RGListVinsCheckPresence(ctx context.Context, plan *models.DataSourceRGListVinsModel, c *client.Client) (*rg.ListVINS, error) {
	listVinsReq := rg.ListVINSRequest{
		RGID: uint64(plan.RGID.ValueInt64()),
	}

	if !plan.Name.IsNull() {
		listVinsReq.Name = plan.Name.ValueString()
	}
	if !plan.AccountID.IsNull() {
		listVinsReq.AccountID = uint64(plan.AccountID.ValueInt64())
	}
	if !plan.ExtIP.IsNull() {
		listVinsReq.ExtIP = plan.ExtIP.ValueString()
	}
	if !plan.VINSID.IsNull() {
		listVinsReq.VINSID = uint64(plan.VINSID.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		listVinsReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		listVinsReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		listVinsReq.Size = uint64(plan.Size.ValueInt64())
	}

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

	tflog.Info(ctx, "RGListVinsCheckPresence: response from CloudBroker().RG().ListVINS", map[string]any{"response": rgListVins})

	return rgListVins, err
}
