package k8s

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/extnet"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8ci"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/rg"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func existK8sID(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
	c := m.(*controller.ControllerCfg)
	k8sID := uint64(d.Get("k8s_id").(int))
	req := k8s.ListRequest{}

	k8sList, err := c.CloudAPI().K8S().List(ctx, req)
	if err != nil {
		return false, err
	}

	return len(k8sList.FilterByID(k8sID).Data) != 0, nil
}

func existK8sCIID(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
	c := m.(*controller.ControllerCfg)
	k8sciID := uint64(d.Get("k8sci_id").(int))
	req := k8ci.ListRequest{}

	k8sciList, err := c.CloudAPI().K8CI().List(ctx, req)
	if err != nil {
		return false, err
	}

	return len(k8sciList.FilterByID(k8sciID).Data) != 0, nil
}

func existRGID(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
	c := m.(*controller.ControllerCfg)
	rgID := uint64(d.Get("rg_id").(int))
	req := rg.ListRequest{}

	rgList, err := c.CloudAPI().RG().List(ctx, req)
	if err != nil {
		return false, err
	}

	return len(rgList.FilterByID(rgID).Data) != 0, nil
}

func existExtNetID(ctx context.Context, d *schema.ResourceData, m interface{}) (bool, error) {
	extNetID := uint64(d.Get("extnet_id").(int))

	if extNetID == 0 {
		return true, nil
	}

	c := m.(*controller.ControllerCfg)
	req := extnet.ListRequest{}

	extNetList, err := c.CloudAPI().ExtNet().List(ctx, req)
	if err != nil {
		return false, err
	}

	return len(extNetList.FilterByID(extNetID).Data) != 0, nil
}
