package vins

import (
	"context"
	"fmt"

	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
	"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/vins/models"
)

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

	if plan.RGID.IsUnknown() && plan.AccountID.IsUnknown() {
		tflog.Error(ctx, "resourceVINSInputChecks: Error providing rg_id or account_id")
		diags.AddError(fmt.Sprintf("Unable to validate rg_id and account_id"), "Either accountId or resource group ID should be specified")
		return diags
	}

	if !plan.RGID.IsUnknown() && !plan.AccountID.IsUnknown() {
		tflog.Error(ctx, "resourceVINSInputChecks: Error providing rg_id and account_id at the same time")
		diags.AddError(fmt.Sprintf("Unable to validate rg_id and account_id"), "Either rg_id or account_id must be provided")
		return diags
	}

	if !plan.RGID.IsUnknown() { // RGID is optional & computed
		rgId := uint64(plan.RGID.ValueInt64())
		tflog.Info(ctx, "resourceVINSInputChecks: exist resource group", map[string]any{"rg_id": rgId})
		err := ic.ExistRG(ctx, rgId, c)
		if err != nil {
			diags.AddError(fmt.Sprintf("Cannot get info about rg with ID %v", rgId), err.Error())
		}
	}

	if !plan.ExtNet.IsNull() { // ExtNet is optional
		var extnetPlan models.ExtNetModel
		tflog.Info(ctx, "resourceVINSInputChecks: new extnet specified", map[string]any{"name": plan.Name.ValueString()})
		diags.Append(plan.ExtNet.As(ctx, &extnetPlan, basetypes.ObjectAsOptions{UnhandledNullAsEmpty: true})...)
		if diags.HasError() {
			tflog.Error(ctx, "resourceVINSInputChecks: cannot populate extnet with plan.ExtNet object element")
			return diags
		}

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

	if !plan.AccountID.IsUnknown() { // AccountID is optional & computed
		accountId := uint64(plan.AccountID.ValueInt64())
		tflog.Info(ctx, "resourceVINSInputChecks: 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())
		}
	}

	if !plan.GID.IsUnknown() { // GID is optional & computed
		gid := uint64(plan.GID.ValueInt64())
		tflog.Info(ctx, "resourceVINSInputChecks: 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())
		}
	}

	return diags
}

// resourceVINSStaticRouteInputChecks checks if user provided vins_id is valid.
func resourceVINSStaticRouteInputChecks(ctx context.Context, plan *models.ResourceVINSStaticRouteModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	vinsId := uint64(plan.VinsID.ValueInt64())
	tflog.Info(ctx, "resourceVINSStaticRouteInputChecks: exist vins", map[string]any{"vins_id": vinsId})
	err := ic.ExistVins(ctx, vinsId, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about vins with ID %v", vinsId), err.Error())
	}

	return diags
}
