package utilities

import (
	"context"
	"fmt"

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

func BServiceListDataSourceCheckPresence(ctx context.Context, plan models.ListBasicServicesModel, c *client.Client) (*bservice.ListBasicServices, error) {
	tflog.Info(ctx, "BServiceListDataSourceCheckPresence: Get list bserivce")

	accListReq := bservice.ListRequest{}

	if !plan.ByID.IsNull() {
		accListReq.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		accListReq.Name = plan.Name.ValueString()
	}
	if !plan.RGName.IsNull() {
		accListReq.RGName = plan.RGName.ValueString()
	}
	if !plan.Status.IsNull() {
		accListReq.Status = plan.Status.ValueString()
	}
	if !plan.TechStatus.IsNull() {
		accListReq.TechStatus = plan.TechStatus.ValueString()
	}
	if !plan.AccountName.IsNull() {
		accListReq.AccountName = plan.AccountName.ValueString()
	}
	if !plan.AccountID.IsNull() {
		accListReq.AccountID = uint64(plan.AccountID.ValueInt64())
	}
	if !plan.RGID.IsNull() {
		accListReq.RGID = uint64(plan.RGID.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		accListReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		accListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		accListReq.Size = uint64(plan.Size.ValueInt64())
	}

	record, err := c.CloudAPI().BService().List(ctx, accListReq)

	if err != nil {
		return nil, fmt.Errorf("cannot get bservice list with error: %w", err)
	}

	return record, err
}
