package flattens

import (
	"context"
	"fmt"

	"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/decort-golang-sdk/pkg/cloudbroker/vins"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/models"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)

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

	diags := diag.Diagnostics{}

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

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

	id := uuid.New()
	*state = models.DataSourceVINSListModel{
		ByID:           state.ByID,
		Name:           state.Name,
		AccountID:      state.AccountID,
		RGID:           state.RGID,
		ExtIP:          state.ExtIP,
		VNFDevID:       state.VNFDevID,
		IncludeDeleted: state.IncludeDeleted,
		SortBy:         state.SortBy,
		Page:           state.Page,
		Size:           state.Size,
		Timeouts:       state.Timeouts,

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

	items := make([]models.ItemVinsModel, 0, len(vinsList.Data))
	for _, item := range vinsList.Data {
		v := models.ItemVinsModel{
			AccountID:          types.Int64Value(int64(item.AccountID)),
			AccountName:        types.StringValue(item.AccountName),
			CreatedBy:          types.StringValue(item.CreatedBy),
			CreatedTime:        types.Int64Value(int64(item.CreatedTime)),
			DefaultGW:          types.StringValue(item.DefaultGW),
			DefaultQOS:         flattenQOS(ctx, &item.DefaultQOS),
			DeletedBy:          types.StringValue(item.DeletedBy),
			DeletedTime:        types.Int64Value(int64(item.DeletedTime)),
			Description:        types.StringValue(item.Description),
			ExternalIP:         types.StringValue(item.ExternalIP),
			ExtnetID:           types.Int64Value(int64(item.ExtnetId)),
			FreeIPs:            types.Int64Value(int64(item.FreeIPs)),
			GID:                types.Int64Value(int64(item.GID)),
			GUID:               types.Int64Value(int64(item.GUID)),
			ID:                 types.Int64Value(int64(item.ID)),
			LockStatus:         types.StringValue(item.LockStatus),
			ManagerID:          types.Int64Value(int64(item.ManagerID)),
			ManagerType:        types.StringValue(item.ManagerType),
			Milestones:         types.Int64Value(int64(item.Milestones)),
			Name:               types.StringValue(item.Name),
			Netmask:            types.Int64Value(int64(item.NetMask)),
			Network:            types.StringValue(item.Network),
			PreReservationsNum: types.Int64Value(int64(item.PreReservationsNum)),
			PriVNFDevID:        types.Int64Value(int64(item.PriVNFDevID)),
			Redundant:          types.BoolValue(item.Redundant),
			RGID:               types.Int64Value(int64(item.RGID)),
			RGName:             types.StringValue(item.RGName),
			SecVNFDefID:        types.Int64Value(int64(item.SecVNFDevID)),
			Status:             types.StringValue(item.Status),
			UpdatedBy:          types.StringValue(item.UpdatedBy),
			UpdatedTime:        types.Int64Value(int64(item.UpdatedTime)),
			UserManaged:        types.BoolValue(item.UserManaged),
			VNFs:               flattenListVNFs(ctx, &item.VNFs),
			VXLANID:            types.Int64Value(int64(item.VXLANID)),
		}
		items = append(items, v)
	}
	state.Items = items

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

// flattenListVNFs flattens ItemListVNFs.
// Flatten errors are added to tflog.
func flattenListVNFs(ctx context.Context, iv *vins.ItemVNFs) types.Object {
	tflog.Info(ctx, "Start flattenListVNFs")

	temp := models.ListVNFsModel{
		DHCP: types.Int64Value(int64(iv.DHCP)),
		DNS:  types.Int64Value(int64(iv.DNS)),
		FW:   types.Int64Value(int64(iv.FW)),
		GW:   types.Int64Value(int64(iv.GW)),
		NAT:  types.Int64Value(int64(iv.NAT)),
		VPN:  types.Int64Value(int64(iv.VPN)),
	}

	res, err := types.ObjectValueFrom(ctx, models.ItemListVNFs, temp)
	if err != nil {
		tflog.Error(ctx, fmt.Sprint("Error flattenListVNFs struct to obj", err))
	}

	tflog.Info(ctx, "End flattenListVNFs")
	return res
}
