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 AccountTemplatesListCheckPresence(ctx context.Context, plan *models.DataSourceAccountTemplatesListModel, c *client.Client) (*account.ListTemplates, error) {
	tflog.Info(ctx, "AccountTemplatesListCheckPresence: Get info about account templates list")

	tempListReq := account.ListTemplatesRequest{
		AccountID: uint64(plan.AccountID.ValueInt64()),
	}

	if !plan.IncludeDeleted.IsNull() {
		tempListReq.IncludeDeleted = plan.IncludeDeleted.ValueBool()
	}
	if !plan.ImageID.IsNull() {
		tempListReq.ImageID = uint64(plan.ImageID.ValueInt64())
	}
	if !plan.Name.IsNull() {
		tempListReq.Name = plan.Name.ValueString()
	}
	if !plan.Type.IsNull() {
		tempListReq.Type = plan.Type.ValueString()
	}
	if !plan.Page.IsNull() {
		tempListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		tempListReq.Size = uint64(plan.Size.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		tempListReq.SortBy = plan.SortBy.ValueString()
	}

	tflog.Info(ctx, "AccountTemplatesListCheckPresence: before call CloudAPI().Account().ListTemplates", map[string]any{"req": tempListReq})
	tempList, err := c.CloudAPI().Account().ListTemplates(ctx, tempListReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about account templates list with error: %w", err)
	}

	tflog.Info(ctx, "AccountTemplatesListCheckPresence: response from CloudAPI().Account().ListTemplates")

	return tempList, err
}
