package secgroup

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/secgroup"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func utilitySecurityGroupListCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*secgroup.ListSecurityGroups, error) {
	c := m.(*controller.ControllerCfg)

	req := secgroup.ListRequest{}

	if size, ok := d.GetOk("size"); ok {
		req.Size = uint64(size.(int))
	}
	if page, ok := d.GetOk("page"); ok {
		req.Page = uint64(page.(int))
	}
	if byID, ok := d.GetOk("by_id"); ok {
		req.ByID = uint64(byID.(int))
	}
	if accountID, ok := d.GetOk("account_id"); ok {
		req.AccountID = uint64(accountID.(int))
	}
	if name, ok := d.GetOk("name"); ok {
		req.Name = name.(string)
	}
	if desc, ok := d.GetOk("desc"); ok {
		req.Description = desc.(string)
	}
	if sortBy, ok := d.GetOk("sort_by"); ok {
		req.SortBy = sortBy.(string)
	}
	if createdMin, ok := d.GetOk("created_min"); ok {
		req.CreatedMin = uint64(createdMin.(int))
	}
	if createdMax, ok := d.GetOk("created_max"); ok {
		req.CreatedMax = uint64(createdMax.(int))
	}
	if updatedMin, ok := d.GetOk("updated_min"); ok {
		req.UpdatedMin = uint64(updatedMin.(int))
	}
	if updatedMax, ok := d.GetOk("updated_max"); ok {
		req.UpdatedMax = uint64(updatedMax.(int))
	}

	log.Debugf("utilitySecurityGroupListCheckPresence: load storage policy list")

	securityGroupList, err := c.CloudAPI().SecurityGroup().List(ctx, req)
	if err != nil {
		return nil, err
	}

	return securityGroupList, nil

}
