package flipgroup

import (
	"context"
	"fmt"
	"strings"

	"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/cloudapi/flipgroup/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/ic"
)

// resourceFlipgroupInputChecks checks if user provided, account_id and net_id are valid.
// It also checks that either account_id or net_id is specified.
func resourceFlipgroupInputChecks(ctx context.Context, plan *models.ResourceFLIPGroupModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

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

	netType := plan.NetType.ValueString()
	netId := uint64(plan.NetID.ValueInt64())

	if strings.EqualFold(netType, "EXTNET") {
		tflog.Info(ctx, "resourceFlipgroupInputChecks: exist extnet id", map[string]any{"extnet_id": netId})
		err := ic.ExistExtNet(ctx, netId, c)
		if err != nil {
			diags.AddError(fmt.Sprintf("Cannot get info about extnet with ID %v", netId), err.Error())
		}
	} else {
		tflog.Info(ctx, "resourceFlipgroupInputChecks: exist VINS id", map[string]any{"vins_id": netId})
		err := ic.ExistVins(ctx, netId, c)
		if err != nil {
			diags.AddError(fmt.Sprintf("Cannot get info about VINS with ID %v", netId), err.Error())
		}
	}

	return diags
}
