package utilities

import (
	"context"
	"fmt"

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

func AccountVinsListCheck(ctx context.Context, plan *models.DataSourceAccountVinsListModel, c *client.Client) (*account.ListVINS, error) {
	tflog.Info(ctx, "AccountVinsListCheck: Get info about list vins")

	vinsListReq := account.ListVINSRequest{}

	if !plan.AccountID.IsNull() {
		vinsListReq.AccountID = uint64(plan.AccountID.ValueInt64())
	}
	if !plan.VinsID.IsNull() {
		vinsListReq.VINSID = uint64(plan.VinsID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		vinsListReq.Name = plan.Name.ValueString()
	}
	if !plan.RGID.IsNull() {
		vinsListReq.RGID = uint64(plan.RGID.ValueInt64())
	}
	if !plan.ExtIp.IsNull() {
		vinsListReq.ExtIP = plan.ExtIp.ValueString()
	}
	if !plan.SortBy.IsNull() {
		vinsListReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		vinsListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		vinsListReq.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "AccountVinsListCheck: before call CloudAPI().Account().ListVINS", map[string]any{"req": vinsListReq})
	vinsList, err := c.CloudAPI().Account().ListVINS(ctx, vinsListReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about vins list with error: %w", err)
	}

	tflog.Info(ctx, "AccountVinsListCheck: response from CloudAPI().Account().ListVINS")

	return vinsList, err
}
