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 LBFrontendResource(ctx context.Context, plan *models.ResourceLBFrontendModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.LBFrontendResource")

	diags := diag.Diagnostics{}

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

	*plan = models.ResourceLBFrontendModel{
		LBID:     plan.LBID,
		Name:     plan.Name,
		Backend:  plan.Backend,
		ID:       plan.ID,
		Timeouts: plan.Timeouts,
		GUID:     types.StringValue(recordItemFrontend.GUID),
		Bindings: flattenBindingsInLB(ctx, recordItemFrontend.Bindings),
	}

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