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

func LBListDataSource(ctx context.Context, state *models.DataSourceLBList, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.LBListDataSource")

	diags := diag.Diagnostics{}

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

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

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

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

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

		item := models.ItemsLBListModel{
			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),
			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)),
			ImageID:       types.Int64Value(int64(lbItem.ImageID)),
			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.LBListDataSource: after flatten")

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