package utilities

import (
	"context"
	"fmt"

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

func AccountFlipgroupsListCheckPresence(ctx context.Context, plan *models.DataSourceAccountFlipgroupsListModel, c *client.Client) (*account.ListFLIPGroups, error) {
	tflog.Info(ctx, "AccountFlipgroupsListCheckPresence: Get info about account flipgroups list")

	flipgroupsListReq := account.ListFLIPGroupsRequest{AccountID: uint64(plan.AccountID.ValueInt64())}

	if !plan.Name.IsNull() {
		flipgroupsListReq.Name = plan.Name.ValueString()
	}
	if !plan.VINSID.IsNull() {
		flipgroupsListReq.VINSID = uint64(plan.VINSID.ValueInt64())
	}
	if !plan.VINSName.IsNull() {
		flipgroupsListReq.VINSName = plan.VINSName.ValueString()
	}
	if !plan.ExtNetID.IsNull() {
		flipgroupsListReq.ExtNetID = uint64(plan.ExtNetID.ValueInt64())
	}
	if !plan.ByIP.IsNull() {
		flipgroupsListReq.ByIP = plan.ByIP.ValueString()
	}
	if !plan.FLIPGroupID.IsNull() {
		flipgroupsListReq.FLIPGroupID = uint64(plan.FLIPGroupID.ValueInt64())
	}
	if !plan.SortBy.IsNull() {
		flipgroupsListReq.SortBy = plan.SortBy.ValueString()
	}
	if !plan.Page.IsNull() {
		flipgroupsListReq.Page = uint64(plan.Page.ValueInt64())
	}
	if !plan.Size.IsNull() {
		flipgroupsListReq.Size = uint64(plan.Size.ValueInt64())
	}

	tflog.Info(ctx, "AccountListCheckPresence: before call CloudBroker().Account().ListFLIPGroups", map[string]any{"req": flipgroupsListReq})
	flipgroupsList, err := c.CloudBroker().Account().ListFLIPGroups(ctx, flipgroupsListReq)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about account flipgroups list with error: %w", err)
	}

	tflog.Info(ctx, "AccountListCheckPresence: response from CloudBroker().Account().ListFLIPGroups")

	return flipgroupsList, err
}
