package utilities

import (
	"context"
	"fmt"

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

func RGListCheckPresence(ctx context.Context, plan *models.DataSourceRGListModel, c *client.Client) (*rg.ListResourceGroups, error) {
	listReq := rg.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.AccountName.IsNull() {
		listReq.AccountName = plan.AccountName.ValueString()
	}
	if !plan.CreatedAfter.IsNull() {
		listReq.CreatedAfter = uint64(plan.CreatedAfter.ValueInt64())
	}
	if !plan.CreatedBefore.IsNull() {
		listReq.CreatedBefore = uint64(plan.CreatedBefore.ValueInt64())
	}
	if !plan.Status.IsNull() {
		listReq.Status = plan.Status.ValueString()
	}
	if !plan.LockStatus.IsNull() {
		listReq.LockStatus = plan.LockStatus.ValueString()
	}
	if !plan.IncludeDeleted.IsNull() {
		listReq.IncludeDeleted = plan.IncludeDeleted.ValueBool()
	}
	if !plan.SortBy.IsNull() {
		listReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		listReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		listReq.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "RGListCheckPresence: before call CloudAPI().RG().List", map[string]any{"response": listReq})
	rgList, err := c.CloudAPI().RG().List(ctx, listReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about resource group with error: %w", err)
	}

	tflog.Info(ctx, "RGListCheckPresence: response from CloudAPI().RG().List", map[string]any{"response": rgList})

	return rgList, err
}
