package flattens

import (
	"context"
	"fmt"

	"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/decort-golang-sdk/pkg/cloudapi/lb"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/lb/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/lb/utilities"
)

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

	diags := diag.Diagnostics{}

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

	*plan = models.ResourceLBBackendModel{
		LBID:      plan.LBID,
		Name:      types.StringValue(recordItemBackend.Name),
		ID:        plan.ID,
		Timeouts:  plan.Timeouts,
		GUID:      types.StringValue(recordItemBackend.GUID),
		Algorithm: types.StringValue(recordItemBackend.Algorithm),
		DownInter: types.Int64Value(int64(recordItemBackend.ServerDefaultSettings.DownInter)),
		Fall:      types.Int64Value(int64(recordItemBackend.ServerDefaultSettings.Fall)),
		Inter:     types.Int64Value(int64(recordItemBackend.ServerDefaultSettings.Inter)),
		MaxConn:   types.Int64Value(int64(recordItemBackend.ServerDefaultSettings.MaxConn)),
		MaxQueue:  types.Int64Value(int64(recordItemBackend.ServerDefaultSettings.MaxQueue)),
		Rise:      types.Int64Value(int64(recordItemBackend.ServerDefaultSettings.Rise)),
		SlowStart: types.Int64Value(int64(recordItemBackend.ServerDefaultSettings.SlowStart)),
		Weight:    types.Int64Value(int64(recordItemBackend.ServerDefaultSettings.Weight)),
		Servers:   flattenServersInLB(ctx, recordItemBackend.Servers),
	}

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

func flattenServersInLB(ctx context.Context, servers []lb.ItemServer) types.List {
	tflog.Info(ctx, "Start flattenServersInLBBackend")
	tempSlice := make([]types.Object, 0, len(servers))
	for _, server := range servers {
		s := models.RecordServerModel{
			Address:        types.StringValue(server.Address),
			Check:          types.StringValue(server.Check),
			GUID:           types.StringValue(server.GUID),
			Name:           types.StringValue(server.Name),
			Port:           types.Int64Value(int64(server.Port)),
			ServerSettings: flattenServersSettings(ctx, server.ServerSettings),
		}
		obj, diags := types.ObjectValueFrom(ctx, models.ItemServers, s)
		if diags != nil {
			tflog.Error(ctx, fmt.Sprint("Error flattenServersInLBBackend struct to obj", diags))
		}
		tempSlice = append(tempSlice, obj)
	}

	res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemServers}, tempSlice)
	if diags != nil {
		tflog.Error(ctx, fmt.Sprint("Error flattenServersInLBBackend", diags))
	}

	tflog.Info(ctx, "End flattenServersInLBBackend")
	return res
}

func flattenServersSettings(ctx context.Context, settings lb.RecordServerSettings) types.Object {
	tflog.Info(ctx, "Start flattenServersSettings")
	s := models.RecordServerSettingsModel{
		Inter:     types.Int64Value(int64(settings.Inter)),
		GUID:      types.StringValue(settings.GUID),
		DownInter: types.Int64Value(int64(settings.DownInter)),
		Rise:      types.Int64Value(int64(settings.Rise)),
		Fall:      types.Int64Value(int64(settings.Fall)),
		SlowStart: types.Int64Value(int64(settings.SlowStart)),
		MaxConn:   types.Int64Value(int64(settings.MaxConn)),
		MaxQueue:  types.Int64Value(int64(settings.MaxQueue)),
		Weight:    types.Int64Value(int64(settings.Weight)),
	}
	obj, diags := types.ObjectValueFrom(ctx, models.ItemServerSettings, s)
	if diags != nil {
		tflog.Error(ctx, fmt.Sprint("Error flattenServersSettings struct to obj", diags))
	}
	tflog.Info(ctx, "End flattenServersSettings")
	return obj
}
