package rg

import (
	"context"
	"fmt"

	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/ic"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/rg/models"
)

func resourceRgInputChecks(ctx context.Context, plan *models.ResourceRGModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	accountId := uint64(plan.AccountID.ValueInt64())
	tflog.Info(ctx, "resourceRgInputChecks: exist account check", map[string]any{"account_id": accountId})
	err := ic.ExistAccount(ctx, accountId, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about account with ID %v", accountId), err.Error())
	}

	gid := uint64(plan.GID.ValueInt64())
	tflog.Info(ctx, "resourceRgInputChecks: exist gid check", map[string]any{"gid": gid})
	err = ic.ExistGID(ctx, gid, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about gid with ID %v", gid), err.Error())
	}

	if !plan.ExtNetID.IsNull() {
		extnetId := uint64(plan.ExtNetID.ValueInt64())
		tflog.Info(ctx, "resourceRgInputChecks: exist ext_net check", map[string]any{"ext_net_id": extnetId})
		err = ic.ExistExtNetInRG(ctx, extnetId, accountId, c)
		if err != nil {
			diags.AddError(fmt.Sprintf("Cannot get info about ext net with ID %v", extnetId), err.Error())
		}
	}

	return diags
}
