package utilities

import (
	"context"

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

func ComputePciDeviceListCheckPresence(ctx context.Context, state *models.ListPCIDevicesModel, c *client.Client) (*compute.ListPCIDevices, error) {
	tflog.Info(ctx, "Get compute pci device list info")

	req := compute.ListPCIDeviceRequest{
		ComputeID: uint64(state.ComputeID.ValueInt64()),
	}

	if !state.RGID.IsNull() {
		req.RGID = uint64(state.RGID.ValueInt64())
	}
	if !state.DevID.IsNull() {
		req.DevID = uint64(state.DevID.ValueInt64())
	}
	if !state.Name.IsNull() {
		req.Name = state.Name.ValueString()
	}
	if !state.Status.IsNull() {
		req.Status = state.Status.ValueString()
	}
	if !state.SortBy.IsNull() {
		req.SortBy = state.SortBy.ValueString()
	}
	if !state.Page.IsNull() {
		req.Page = uint64(state.Page.ValueInt64())
	}
	if !state.Size.IsNull() {
		req.Size = uint64(state.Size.ValueInt64())
	}

	tflog.Info(ctx, "Check req", map[string]any{"req": req})

	log, err := c.CloudAPI().Compute().ListPCIDevice(ctx, req)
	if err != nil {
		return nil, err
	}

	tflog.Info(ctx, "Getting compute pci device list info, successfully")
	return log, nil
}
