package flattens

import (
	"context"

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

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

	diags := diag.Diagnostics{}

	recordFG, diags := utilities.FlipgroupListDataSourceCheckPresence(ctx, state, c)
	if diags.HasError() {
		return diags
	}

	id := uuid.New()
	*state = models.DataSourceFLIPGroupModelList{
		FlipgroupID: state.FlipgroupID,
		Name:        state.Name,
		VinsID:      state.VinsID,
		VinsName:    state.VinsName,
		ExtnetID:    state.ExtnetID,
		ByIP:        state.ByIP,
		AccountID:   state.AccountID,
		RgID:        state.RgID,
		SortBy:      state.SortBy,
		Page:        state.Page,
		Size:        state.Size,
		Timeouts:    state.Timeouts,
		ConnID:      state.ConnID,
		CliendIDs:   state.CliendIDs,
		Status:      state.Status,

		ID:         types.StringValue(id.String()),
		EntryCount: types.Int64Value(int64(recordFG.EntryCount)),
	}

	items := make([]models.ItemFLIPGroupModel, 0, len(recordFG.Data))
	for _, item := range recordFG.Data {
		fg := models.ItemFLIPGroupModel{
			CKey:        types.StringValue(item.CKey),
			Meta:        flattens.FlattenSimpleTypeToList(ctx, types.StringType, item.Meta),
			AccountID:   types.Int64Value(int64(item.AccountID)),
			ClientIDs:   flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, item.Meta),
			ClientType:  types.StringValue(item.ClientType),
			ConnID:      types.Int64Value(int64(item.ConnID)),
			ConnType:    types.StringValue(item.ConnType),
			DefaultGW:   types.StringValue(item.DefaultGW),
			Description: types.StringValue(item.Description),
			GID:         types.Int64Value(int64(item.GID)),
			GUID:        types.Int64Value(int64(item.GUID)),
			ID:          types.Int64Value(int64(item.ID)),
			IP:          types.StringValue(item.IP),
			Milestones:  types.Int64Value(int64(item.Milestones)),
			Name:        types.StringValue(item.Name),
			NetID:       types.Int64Value(int64(item.NetID)),
			NetType:     types.StringValue(item.NetType),
			NetMask:     types.Int64Value(int64(item.NetMask)),
			Status:      types.StringValue(item.Status),
		}
		items = append(items, fg)
	}
	state.Items = items

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