package image

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/dc"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/service/cloudbroker/ic"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func checkParamsExistenceBlankCompute(ctx context.Context, d *schema.ResourceData, c *controller.ControllerCfg) diag.Diagnostics {
	var errs []error

	accountID := uint64(d.Get("account_id").(int))
	computeId := uint64(d.Get("compute_id").(int))

	if err := ic.ExistBlankCompute(ctx, computeId, c); err != nil {
		errs = append(errs, err)
	}

	if _, ok := d.GetOk("account_id"); ok {
		if err := ic.ExistAccount(ctx, accountID, c); err != nil {
			errs = append(errs, err)
		}
	}

	return dc.ErrorsToDiagnostics(errs)
}

func checkParamsExistencePlatformDisk(ctx context.Context, d *schema.ResourceData, c *controller.ControllerCfg) diag.Diagnostics {
	var errs []error

	accountID := uint64(d.Get("account_id").(int))
	diskId := uint64(d.Get("disk_id").(int))

	if err := ic.ExistPlatformDisk(ctx, diskId, c); err != nil {
		errs = append(errs, err)
	}

	if _, ok := d.GetOk("account_id"); ok {
		if err := ic.ExistAccount(ctx, accountID, c); err != nil {
			errs = append(errs, err)
		}
	}

	return dc.ErrorsToDiagnostics(errs)
}
