package utilities

import (
	"context"

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

func K8CIListDataSourceCheckPresence(ctx context.Context, plan *models.DataSourceK8CIListModel, c *client.Client) (*k8ci.ListK8CI, diag.Diagnostics) {
	tflog.Info(ctx, "K8CIListDataSourceCheckPresence: Get info about k8ci list")

	diags := diag.Diagnostics{}

	listReq := k8ci.ListRequest{}
	if !plan.ByID.IsNull() {
		listReq.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		listReq.Name = plan.Name.ValueString()
	}
	if !plan.Status.IsNull() {
		listReq.Status = plan.Status.ValueString()
	}
	if !plan.WorkerDriver.IsNull() {
		listReq.WorkerDriver = plan.WorkerDriver.ValueString()
	}
	if !plan.MasterDriver.IsNull() {
		listReq.MasterDriver = plan.MasterDriver.ValueString()
	}
	if !plan.NetworkPlugin.IsNull() {
		listReq.NetworkPlugins = plan.NetworkPlugin.ValueString()
	}
	if !plan.IncludeDisabled.IsNull() {
		listReq.IncludeDisabled = plan.IncludeDisabled.ValueBool()
	}
	if !plan.SortBy.IsNull() {
		listReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		listReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		listReq.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "K8CIListDataSourceCheckPresence: before call CloudBroker().K8CI().List", map[string]any{"req": listReq})
	list, err := c.CloudBroker().K8CI().List(ctx, listReq)
	if err != nil {
		diags.AddError("Cannot get info about k8ci list", err.Error())
		return nil, diags
	}
	tflog.Info(ctx, "K8CIListDataSourceCheckPresence: successfull response from CloudBroker().K8CI().List")

	return list, nil
}
