package flattens

import (
	"context"

	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"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/lb/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/lb/utilities"
)

func LBBackendServerResource(ctx context.Context, plan *models.ResourceLBBackendServerModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.LBBackendServerResource")

	diags := diag.Diagnostics{}

	recordItemServer, diags := utilities.LBBackendServerResourceCheckPresence(ctx, plan, c)
	if diags.HasError() {
		return diags
	}

	*plan = models.ResourceLBBackendServerModel{
		LBID:      plan.LBID,
		Backend:   plan.Backend,
		Name:      plan.Name,
		Address:   plan.Address,
		Port:      plan.Port,
		ID:        plan.ID,
		Timeouts:  plan.Timeouts,
		Check:     types.StringValue(recordItemServer.Check),
		Inter:     types.Int64Value(int64(recordItemServer.ServerSettings.Inter)),
		DownInter: types.Int64Value(int64(recordItemServer.ServerSettings.DownInter)),
		Rise:      types.Int64Value(int64(recordItemServer.ServerSettings.Rise)),
		Fall:      types.Int64Value(int64(recordItemServer.ServerSettings.Fall)),
		SlowStart: types.Int64Value(int64(recordItemServer.ServerSettings.SlowStart)),
		MaxConn:   types.Int64Value(int64(recordItemServer.ServerSettings.MaxConn)),
		MaxQueue:  types.Int64Value(int64(recordItemServer.ServerSettings.MaxQueue)),
		Weight:    types.Int64Value(int64(recordItemServer.ServerSettings.Weight)),
	}

	tflog.Info(ctx, "End flattens.LBBackendServerResource", map[string]any{"name": plan.Name.ValueString()})
	return nil
}
