package flattens

import (
	"context"
	"encoding/json"

	"github.com/google/uuid"
	"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 LBListDeletedDataSource(ctx context.Context, state *models.DataSourceLBListDeleted, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.LBListDeletedDataSource")

	diags := diag.Diagnostics{}

	lbList, err := utilities.LBListDeletedDataSourceCheckPresence(ctx, state, c)
	if err != nil {
		diags.AddError("Cannot get info about list deleted", err.Error())
		return diags
	}

	tflog.Info(ctx, "flattens.LBListDeletedDataSource: before flatten")

	id := uuid.New()
	*state = models.DataSourceLBListDeleted{
		ByID:       state.ByID,
		Name:       state.Name,
		AccountID:  state.AccountID,
		RgID:       state.RgID,
		TechStatus: state.TechStatus,
		FrontIP:    state.FrontIP,
		BackIP:     state.BackIP,
		SortBy:     state.SortBy,
		Page:       state.Page,
		Size:       state.Size,
		Timeouts:   state.Timeouts,

		Id: types.StringValue(id.String()),
	}

	items := make([]models.ItemsLBListDeletedModel, 0, len(lbList.Data))
	for _, lbItem := range lbList.Data {
		acl, _ := json.Marshal(lbItem.ACL)

		item := models.ItemsLBListDeletedModel{
			HAMode:        types.BoolValue(lbItem.HAMode),
			ACL:           types.StringValue(string(acl)),
			BackendHAIP:   types.StringValue(lbItem.BackendHAIP),
			Backends:      flattenBackendsInLB(ctx, lbItem.Backends),
			CreatedBy:     types.StringValue(lbItem.CreatedBy),
			CreatedTime:   types.Int64Value(int64(lbItem.CreatedTime)),
			DeletedBy:     types.StringValue(lbItem.DeletedBy),
			DeletedTime:   types.Int64Value(int64(lbItem.DeletedTime)),
			Description:   types.StringValue(lbItem.Description),
			DPAPIUser:     types.StringValue(lbItem.DPAPIUser),
			DPAPIPassword: types.StringValue(lbItem.DPAPIPassword),
			ExtNetID:      types.Int64Value(int64(lbItem.ExtNetID)),
			FrontendHAIP:  types.StringValue(lbItem.FrontendHAIP),
			Frontends:     flattenFrontendsInLB(ctx, lbItem.Frontends),
			GID:           types.Int64Value(int64(lbItem.GID)),
			GUID:          types.Int64Value(int64(lbItem.GUID)),
			LBID:          types.Int64Value(int64(lbItem.ID)),
			Milestones:    types.Int64Value(int64(lbItem.Milestones)),
			Name:          types.StringValue(lbItem.Name),
			PrimaryNode:   flattenNodeInLB(ctx, lbItem.PrimaryNode),
			RGID:          types.Int64Value(int64(lbItem.RGID)),
			RGName:        types.StringValue(lbItem.RGName),
			SecondaryNode: flattenNodeInLB(ctx, lbItem.SecondaryNode),
			Status:        types.StringValue(lbItem.Status),
			TechStatus:    types.StringValue(lbItem.TechStatus),
			UpdatedBy:     types.StringValue(lbItem.UpdatedBy),
			UpdatedTime:   types.Int64Value(int64(lbItem.UpdatedTime)),
			VINSID:        types.Int64Value(int64(lbItem.VINSID)),
		}

		items = append(items, item)
	}

	state.Items = items
	state.EntryCount = types.Int64Value(int64(lbList.EntryCount))

	tflog.Info(ctx, "flattens.LBListDeletedDataSource: after flatten")

	tflog.Info(ctx, "End flattens.LBListDeletedDataSource")
	return nil
}
