package flattens

import (
	"context"
	"encoding/json"

	"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"
)

// LBDataSource flattens data source for lb.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func LBDataSource(ctx context.Context, state *models.DataSourceLB, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.LBDataSource")

	diags := diag.Diagnostics{}

	lbID := uint64(state.LBID.ValueInt64())

	recordLB, diags := utilities.LBDataSourceCheckPresence(ctx, lbID, c)
	if diags.HasError() {
		return diags
	}

	tflog.Info(ctx, "flattens.LBDataSource: before flatten", map[string]any{"lb_id": lbID})

	acl, _ := json.Marshal(recordLB.ACL)

	*state = models.DataSourceLB{
		LBID:     state.LBID,
		Timeouts: state.Timeouts,

		HAMode:        types.BoolValue(recordLB.HAMode),
		ACL:           types.StringValue(string(acl)),
		BackendHAIP:   types.StringValue(recordLB.BackendHAIP),
		Backends:      flattenBackendsInLB(ctx, recordLB.Backends),
		CreatedBy:     types.StringValue(recordLB.CreatedBy),
		CreatedTime:   types.Int64Value(int64(recordLB.CreatedTime)),
		DeletedBy:     types.StringValue(recordLB.DeletedBy),
		DeletedTime:   types.Int64Value(int64(recordLB.DeletedTime)),
		Description:   types.StringValue(recordLB.Description),
		DPAPIUser:     types.StringValue(recordLB.DPAPIUser),
		ExtNetID:      types.Int64Value(int64(recordLB.ExtNetID)),
		FrontendHAIP:  types.StringValue(recordLB.FrontendHAIP),
		Frontends:     flattenFrontendsInLB(ctx, recordLB.Frontends),
		GID:           types.Int64Value(int64(recordLB.GID)),
		GUID:          types.Int64Value(int64(recordLB.GUID)),
		ID:            types.Int64Value(int64(recordLB.ID)),
		ImageID:       types.Int64Value(int64(recordLB.ImageID)),
		ManagerId:     types.Int64Value(int64(recordLB.ManagerId)),
		ManagerType:   types.StringValue(recordLB.ManagerType),
		Milestones:    types.Int64Value(int64(recordLB.Milestones)),
		Name:          types.StringValue(recordLB.Name),
		PartK8s:       types.BoolValue(recordLB.PartK8s),
		PrimaryNode:   flattenNodeInLB(ctx, recordLB.PrimaryNode),
		RGID:          types.Int64Value(int64(recordLB.RGID)),
		RGName:        types.StringValue(recordLB.RGName),
		SecondaryNode: flattenNodeInLB(ctx, recordLB.SecondaryNode),
		Status:        types.StringValue(recordLB.Status),
		TechStatus:    types.StringValue(recordLB.TechStatus),
		UpdatedBy:     types.StringValue(recordLB.UpdatedBy),
		UpdatedTime:   types.Int64Value(int64(recordLB.UpdatedTime)),
		UserManaged:   types.BoolValue(recordLB.UserManaged),
		VINSID:        types.Int64Value(int64(recordLB.VINSID)),
	}

	tflog.Info(ctx, "End flattens.LBDataSource", map[string]any{"lb_id": state.ID.ValueInt64()})
	return nil
}
