package utilities

import (
	"context"
	"fmt"

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

func RGListDeletedCheckPresence(ctx context.Context, plan *models.DataSourceRGListDeletedModel, c *client.Client) (*rg.ListRG, error) {
	listDelReq := rg.ListDeletedRequest{}

	if !plan.ById.IsNull() {
		listDelReq.ByID = uint64(plan.ById.ValueInt64())
	}
	if !plan.Name.IsNull() {
		listDelReq.Name = plan.Name.ValueString()
	}
	if !plan.AccountId.IsNull() {
		listDelReq.AccountID = uint64(plan.AccountId.ValueInt64())
	}
	if !plan.AccountName.IsNull() {
		listDelReq.AccountName = plan.AccountName.ValueString()
	}
	if !plan.CreatedAfter.IsNull() {
		listDelReq.CreatedAfter = uint64(plan.CreatedAfter.ValueInt64())
	}
	if !plan.CreatedBefore.IsNull() {
		listDelReq.CreatedBefore = uint64(plan.CreatedBefore.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		listDelReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.LockStatus.IsNull() {
		listDelReq.LockStatus = plan.LockStatus.ValueString()
	}
	if !plan.Page.IsNull() {
		listDelReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		listDelReq.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "RGListDeletedCheckPresence: before call CloudBroker().RG().ListDeleted", map[string]any{"response": listDelReq})
	rgListDel, err := c.CloudBroker().RG().ListDeleted(ctx, listDelReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about resource group list deleted with error: %w", err)
	}

	tflog.Info(ctx, "RGListDeletedCheckPresence: response from CloudBroker().RG().ListDeleted", map[string]any{"response": rgListDel})

	return rgListDel, err
}
