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/cloudapi/vins"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/vins/models"
)

func VINSListDataSourceCheckPresence(ctx context.Context, plan *models.DataSourceVINSListModel, c *client.Client) (*vins.ListVINS, diag.Diagnostics) {
	tflog.Info(ctx, "VINSListDataSourceCheckPresence: Get info about vins list")

	diags := diag.Diagnostics{}

	listReq := vins.ListRequest{}
	if !plan.ByID.IsNull() {
		listReq.ByID = uint64(plan.ByID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		listReq.Name = plan.Name.ValueString()
	}
	if !plan.AccountID.IsNull() {
		listReq.AccountID = uint64(plan.AccountID.ValueInt64())
	}
	if !plan.RGID.IsNull() {
		listReq.RGID = uint64(plan.RGID.ValueInt64())
	}
	if !plan.ExtIP.IsNull() {
		listReq.ExtIP = plan.ExtIP.ValueString()
	}
	if !plan.VNFDevID.IsNull() {
		listReq.VNFDevId = uint64(plan.VNFDevID.ValueInt64())
	}
	if !plan.IncludeDeleted.IsNull() {
		listReq.IncludeDeleted = plan.IncludeDeleted.ValueBool()
	}
	if !plan.SortBy.IsNull() {
		listReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Status.IsNull() {
		listReq.Status = plan.Status.ValueString()
	}
	if !plan.Page.IsNull() {
		listReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		listReq.Size = uint64(plan.Size.ValueInt64())
	}

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

	return list, nil
}
