package disks

import (
	"context"

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

func utilityDiskListUnattachedCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*disks.ListUnattachedDisks, error) {
	c := m.(*controller.ControllerCfg)
	req := disks.ListUnattachedRequest{}

	if by_id, ok := d.GetOk("by_id"); ok {
		req.ByID = uint64(by_id.(int))
	}
	if account_name, ok := d.GetOk("account_name"); ok {
		req.AccountName = account_name.(string)
	}
	if disk_max_size, ok := d.GetOk("disk_max_size"); ok {
		req.DiskMaxSize = int64(disk_max_size.(int))
	}
	if status, ok := d.GetOk("status"); ok {
		req.Status = status.(string)
	}
	if accountId, ok := d.GetOk("account_id"); ok {
		req.AccountID = uint64(accountId.(int))
	}
	if sepId, ok := d.GetOk("sep_id"); ok {
		req.SEPID = uint64(sepId.(int))
	}
	if pool, ok := d.GetOk("pool"); ok {
		req.Pool = pool.(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))
	}
	if storagePolicyID, ok := d.GetOk("storage_policy_id"); ok {
		req.StoragePolicyID = uint64(storagePolicyID.(int))
	}

	log.Debugf("utilityDiskListUnattachedCheckPresence: load disk Unattached list")
	unattachedList, err := c.CloudBroker().Disks().ListUnattached(ctx, req)
	if err != nil {
		return nil, err
	}

	return unattachedList, nil
}
