package stpolicy

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

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

	if accountID, ok := d.GetOk("account_id"); ok {
		req.AccountID = uint64(accountID.(int))
	}

	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 name, ok := d.GetOk("name"); ok {
		req.Name = name.(string)
	}

	if status, ok := d.GetOk("status"); ok {
		req.Status = status.(string)
	}

	if desc, ok := d.GetOk("desc"); ok {
		req.Desc = desc.(string)
	}

	if limitIOPS, ok := d.GetOk("limit_iops"); ok {
		req.LimitIOPS = uint64(limitIOPS.(int))
	}

	if sortBy, ok := d.GetOk("sort_by"); ok {
		req.SortBy = sortBy.(string)
	}

	if resgroupID, ok := d.GetOk("resgroup_id"); ok {
		req.ResgroupID = uint64(resgroupID.(int))
	}

	if SEPID, ok := d.GetOk("sep_id"); ok {
		req.SepID = uint64(SEPID.(int))
	}

	if SEPtechstatus, ok := d.GetOk("sep_tech_status"); ok {
		req.SepTechStatus = SEPtechstatus.(string)
	}

	if poolName, ok := d.GetOk("pool_name"); ok {
		req.PoolName = poolName.(string)
	}

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

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

	return storagePolicyList, nil
}
