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 ComputeListDeletedCheckPresence(ctx context.Context, state *models.ListDeletedComputesModel, c *client.Client) (*compute.ListComputes, error) {
	tflog.Info(ctx, "Get compute list deleted info")

	req := compute.ListDeletedRequest{}
	if !state.ByID.IsNull() {
		req.ByID = uint64(state.ByID.ValueInt64())
	}
	if !state.Name.IsNull() {
		req.Name = state.Name.ValueString()
	}
	if !state.AccountID.IsNull() {
		req.AccountID = uint64(state.AccountID.ValueInt64())
	}
	if !state.RGName.IsNull() {
		req.RGName = state.RGName.ValueString()
	}
	if !state.RGID.IsNull() {
		req.RGID = uint64(state.RGID.ValueInt64())
	}
	if !state.TechStatus.IsNull() {
		req.TechStatus = state.TechStatus.ValueString()
	}
	if !state.IPAddress.IsNull() {
		req.IPAddress = state.IPAddress.ValueString()
	}
	if !state.ExtNetName.IsNull() {
		req.ExtNetName = state.ExtNetName.ValueString()
	}
	if !state.ExtNetID.IsNull() {
		req.ExtNetID = uint64(state.ExtNetID.ValueInt64())
	}
	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})

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

	if !state.IgnoreK8s.IsNull() && state.IgnoreK8s.ValueBool() {
		list = matchComputes(list)
	}

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