package lb

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/lb/models"
)

// resourceLBInputChecks checks if rg_id, extnet_id and vins_id are valid.
func resourceLBInputChecks(ctx context.Context, plan *models.ResourceLBModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	extNetId := uint64(plan.ExtNetID.ValueInt64())
	vinsId := uint64(plan.VINSID.ValueInt64())

	if extNetId == 0 && vinsId == 0 {
		diags.AddError(fmt.Sprintf("Unable to validate vins_id and extnet_id"), "vins_id and ext_net_id cannot be both in the value 0")
		return diags
	}

	rgID := uint64(plan.RGID.ValueInt64())
	tflog.Info(ctx, "resourceLBInputChecks: exist resource rg", 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())
	}

	tflog.Info(ctx, "resourceLBInputChecks: exist resource extNet", map[string]any{" extnet_id": extNetId})
	err = ic.ExistExtNetInLb(ctx, extNetId, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about extNet with ID %v", extNetId), err.Error())
	}

	tflog.Info(ctx, "resourceLBInputChecks: exist resource VINS", map[string]any{" vins_id": vinsId})
	err = ic.ExistVinsInLb(ctx, vinsId, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about VINS with ID %v", vinsId), err.Error())
	}

	return diags
}

// resourceLBFrontendBindInputChecks checks if lb_id and backend_name are valid.
func resourceLBFrontendBindInputChecks(ctx context.Context, plan *models.ResourceLBFrontendBindModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	lbID := uint64(plan.LBID.ValueInt64())
	fName := plan.Frontend.ValueString()
	tflog.Info(ctx, "resourceLBFrontendBindInputChecks: exist resource lb", map[string]any{"lb_id": lbID})
	err := ic.ExistLB(ctx, lbID, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", lbID), err.Error())
		return diags
	}
	tflog.Info(ctx, "resourceLBFrontendInputChecks: exist resource lbFrontend", map[string]any{"name": fName})
	err = ic.ExistLBFrontend(ctx, lbID, fName, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about frontend with name %v", fName), err.Error())
		return diags
	}

	return diags
}

// resourceLBFrontendInputChecks checks if lb_id and backend_name are valid.
func resourceLBFrontendInputChecks(ctx context.Context, plan *models.ResourceLBFrontendModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	lbID := uint64(plan.LBID.ValueInt64())
	bName := plan.Backend.ValueString()
	tflog.Info(ctx, "resourceLBFrontendInputChecks: exist resource lb", map[string]any{"lb_id": lbID})
	err := ic.ExistLB(ctx, lbID, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", lbID), err.Error())
		return diags
	}
	tflog.Info(ctx, "resourceLBFrontendInputChecks: exist resource lbBackend", map[string]any{"name": bName})
	err = ic.ExistLBBackend(ctx, lbID, bName, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about backend with name %v", bName), err.Error())
		return diags
	}

	return diags
}

// resourceLBBackendServerInputChecks checks if lb_id and backend_name are valid.
func resourceLBBackendServerInputChecks(ctx context.Context, plan *models.ResourceLBBackendServerModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	lbID := uint64(plan.LBID.ValueInt64())
	bName := plan.Backend.ValueString()
	tflog.Info(ctx, "resourceLBBackendServerInputChecks: exist resource lb", map[string]any{"lb_id": lbID})
	err := ic.ExistLB(ctx, lbID, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", lbID), err.Error())
	} else {
		tflog.Info(ctx, "resourceLBBackendServerInputChecks: exist resource lbBackend", map[string]any{"name": bName})
		err = ic.ExistLBBackend(ctx, lbID, bName, c)
		if err != nil {
			diags.AddError(fmt.Sprintf("Cannot get info about backend with name %v", bName), err.Error())
		}
	}

	return diags
}

// resourceLBBackendInputChecks checks if lb_id are valid.
func resourceLBBackendInputChecks(ctx context.Context, plan *models.ResourceLBBackendModel, c *client.Client) diag.Diagnostics {
	diags := diag.Diagnostics{}

	lbID := uint64(plan.LBID.ValueInt64())
	tflog.Info(ctx, "resourceLBBackendInputChecks: exist resource lb", map[string]any{"lb_id": lbID})
	err := ic.ExistLB(ctx, lbID, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", lbID), err.Error())
	}

	return diags
}
