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

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

	diags := diag.Diagnostics{}

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

	*plan = models.ResourceLBFrontendBindModel{
		Address:  types.StringValue(recordItemFrontendBind.Address),
		Frontend: plan.Frontend,
		LBID:     plan.LBID,
		Name:     types.StringValue(recordItemFrontendBind.Name),
		ID:       plan.ID,
		Timeouts: plan.Timeouts,
		GUID:     types.StringValue(recordItemFrontendBind.GUID),
		Port:     types.Int64Value(int64(recordItemFrontendBind.Port)),
	}

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