package utilities

import (
	"context"
	"fmt"

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

func LBListDataSourceCheckPresence(ctx context.Context, plan *models.DataSourceLBList, c *client.Client) (*lb.ListLB, error) {

	listLBReq := lb.ListRequest{}

	if !plan.ByID.IsNull() {
		listLBReq.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		listLBReq.Name = plan.Name.ValueString()
	}
	if !plan.AccountID.IsNull() {
		listLBReq.AccountID = uint64(plan.AccountID.ValueInt64())
	}
	if !plan.RgID.IsNull() {
		listLBReq.RGID = uint64(plan.RgID.ValueInt64())
	}
	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.Status.IsNull() && !plan.IncludeDeleted.IsNull() {
		listLBReq.IncludeDeleted = plan.IncludeDeleted.ValueBool()
	}
	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, "LBListDataSourceCheckPresence: before call CloudAPI().LB().List", map[string]any{"response": listLBReq})
	lbList, err := c.CloudAPI().LB().List(ctx, listLBReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about data source list lb with error: %w", err)
	}

	tflog.Info(ctx, "LBListDataSourceCheckPresence: response from CloudAPI().LB().List", map[string]any{"response": lbList})

	return lbList, err
}
