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

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

	diags := diag.Diagnostics{}

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

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

	id := uuid.New()
	*state = models.DataSourceAccountFlipgroupsListModel{
		AccountID:   state.AccountID,
		Name:        state.Name,
		VINSID:      state.VINSID,
		VINSName:    state.VINSName,
		ExtNetID:    state.ExtNetID,
		ByIP:        state.ByIP,
		FLIPGroupID: state.FLIPGroupID,
		SortBy:      state.SortBy,
		Page:        state.Page,
		Size:        state.Size,
		Timeouts:    state.Timeouts,

		Id:         types.StringValue(id.String()),
		EntryCount: types.Int64Value(int64(flipgroups.EntryCount)),
	}

	items := make([]models.ItemAccountFlipgroupModel, 0, len(flipgroups.Data))
	for _, item := range flipgroups.Data {
		i := models.ItemAccountFlipgroupModel{
			AccountID:   types.Int64Value(int64(item.AccountID)),
			ClientType:  types.StringValue(item.ClientType),
			ConnType:    types.StringValue(item.ConnType),
			CreatedBy:   types.StringValue(item.CreatedBy),
			CreatedTime: types.Int64Value(int64(item.CreatedTime)),
			DefaultGW:   types.StringValue(item.DefaultGW),
			DeletedBy:   types.StringValue(item.DeletedBy),
			DeletedTime: types.Int64Value(int64(item.DeletedTime)),
			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),
			UpdatedBy:   types.StringValue(item.UpdatedBy),
			UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
		}

		items = append(items, i)
	}
	state.Items = items

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