package trunk

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	log "github.com/sirupsen/logrus"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/trunk"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func utilityTrunkListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*trunk.ListTrunks, error) {
	c := m.(*controller.ControllerCfg)
	req := trunk.ListRequest{}

	if trunkIDs, ok := d.GetOk("trunk_ids"); ok {
		IDs := trunkIDs.([]interface{})
		for _, id := range IDs {
			req.IDs = append(req.IDs, uint64(id.(int)))
		}
	}
	if accountIDs, ok := d.GetOk("account_ids"); ok {
		IDs := accountIDs.([]interface{})
		for _, id := range IDs {
			req.AccountIDs = append(req.AccountIDs, uint64(id.(int)))
		}
	}
	if trunkTags, ok := d.GetOk("trunk_tags"); ok {
		req.TrunkTags = trunkTags.(string)
	}
	if status, ok := d.GetOk("status"); ok {
		req.Status = status.(string)
	}
	if sortBy, ok := d.GetOk("sort_by"); ok {
		req.SortBy = sortBy.(string)
	}
	if page, ok := d.GetOk("page"); ok {
		req.Page = uint64(page.(int))
	}
	if size, ok := d.GetOk("size"); ok {
		req.Size = uint64(size.(int))
	}

	log.Debugf("utilityTrunkListCheckPresence: load trunk network list")
	trunkList, err := c.CloudAPI().Trunk().List(ctx, req)
	if err != nil {
		return nil, err
	}

	return trunkList, nil
}
