package utilities

import (
	"context"
	"fmt"

	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/extnet/models"

	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/extnet"
)

func ExtNetListCheckPresence(ctx context.Context, plan *models.DataSourceExtNetListModel, c *client.Client) (*extnet.ListExtNets, error) {
	tflog.Info(ctx, "ExtNetListCheckPresence: Get info about extnet list")

	extnetListReq := extnet.ListRequest{}

	if !plan.AccountID.IsNull() {
		extnetListReq.AccountID = uint64(plan.AccountID.ValueInt64())
	}
	if !plan.ByID.IsNull() {
		extnetListReq.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		extnetListReq.Name = plan.Name.ValueString()
	}
	if !plan.Network.IsNull() {
		extnetListReq.Network = plan.Network.ValueString()
	}
	if !plan.VLANID.IsNull() {
		extnetListReq.VLANID = uint64(plan.VLANID.ValueInt64())
	}
	if !plan.VNFDevID.IsNull() {
		extnetListReq.VNFDevID = uint64(plan.VNFDevID.ValueInt64())
	}
	if !plan.OVSBridge.IsNull() {
		extnetListReq.OVSBridge = plan.OVSBridge.ValueString()
	}
	if !plan.Status.IsNull() {
		extnetListReq.Status = plan.Status.ValueString()
	}
	if !plan.Page.IsNull() {
		extnetListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		extnetListReq.Size = uint64(plan.Size.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		extnetListReq.SortBy = plan.SortBy.ValueString()
	}

	tflog.Info(ctx, "ExtNetListCheckPresence: before call CloudAPI().ExtNet().List", map[string]any{"req": extnetListReq})
	extNetList, err := c.CloudAPI().ExtNet().List(ctx, extnetListReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about extnet with error: %w", err)
	}

	tflog.Info(ctx, "ExtNetListCheckPresence: response from CloudAPI().ExtNet().List", map[string]any{"response": extNetList})

	return extNetList, err
}
