package utilities

import (
	"context"

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

func K8ciListCheckPresence(ctx context.Context, state *models.K8ciListModel, c *client.Client) (*k8ci.ListK8CI, error) {
	tflog.Info(ctx, "Get k8ci list info")

	req := k8ci.ListRequest{}

	if !state.ByID.IsNull() {
		req.ByID = uint64(state.ByID.ValueInt64())
	}
	if !state.Name.IsNull() {
		req.Name = state.Name.ValueString()
	}
	if !state.Status.IsNull() {
		req.Status = state.Status.ValueString()
	}
	if !state.WorkerDriver.IsNull() {
		req.WorkerDriver = state.WorkerDriver.ValueString()
	}
	if !state.MasterDriver.IsNull() {
		req.MasterDriver = state.MasterDriver.ValueString()
	}
	if !state.NetworkPlugins.IsNull() {
		req.NetworkPlugins = state.NetworkPlugins.ValueString()
	}
	if !state.IncludeDisabled.IsNull() {
		req.IncludeDisabled = state.IncludeDisabled.ValueBool()
	}
	if !state.SortBy.IsNull() {
		req.SortBy = state.SortBy.ValueString()
	}
	if !state.Size.IsNull() {
		req.Size = uint64(state.Size.ValueInt64())
	}
	if !state.Page.IsNull() {
		req.Page = uint64(state.Page.ValueInt64())
	}

	k8sList, err := c.CloudAPI().K8CI().List(ctx, req)
	if err != nil {
		return nil, err
	}

	tflog.Info(ctx, "Getting k8ci list info, successfully")
	return k8sList, nil
}
