package utilities

import (
	"context"
	"fmt"

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

func RGListLBCheckPresence(ctx context.Context, plan *models.DataSourceRGListLBModel, c *client.Client) (*rg.ListLB, error) {
	listLBReq := rg.ListLBRequest{
		RGID: uint64(plan.RGID.ValueInt64()),
	}

	if !plan.ByID.IsNull() {
		listLBReq.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		listLBReq.Name = plan.Name.ValueString()
	}
	if !plan.TechStatus.IsNull() {
		listLBReq.TechStatus = plan.TechStatus.ValueString()
	}
	if !plan.Status.IsNull() {
		listLBReq.Status = plan.Status.ValueString()
	}
	if !plan.FrontIP.IsNull() {
		listLBReq.FrontIP = plan.FrontIP.ValueString()
	}
	if !plan.BackIP.IsNull() {
		listLBReq.BackIP = plan.BackIP.ValueString()
	}
	if !plan.SortBy.IsNull() {
		listLBReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		listLBReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		listLBReq.Size = uint64(plan.Size.ValueInt64())
	}

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

	tflog.Info(ctx, "RGListLBCheckPresence: response from CloudAPI().RG().ListLB", map[string]any{"response": rgListLB})

	return rgListLB, err
}
