package flattens

import (
	"context"
	"fmt"

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

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

	diags := diag.Diagnostics{}

	ID := uint64(state.StackID.ValueInt64())

	recordStack, err := utilities.StackDataSourceCheckPresence(ctx, ID, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about Stack with ID %v", ID), err.Error())
		return diags
	}

	tflog.Info(ctx, "flattens.StackDataSource: before flatten", map[string]any{"stack_id": ID, "recordStack": recordStack})

	*state = models.InfoStackModel{
		StackID:  state.StackID,
		Timeouts: state.Timeouts,

		Ckey:               types.StringValue(recordStack.Ckey),
		Meta:               flattens.FlattenSimpleTypeToList(ctx, types.StringType, recordStack.Meta),
		APIURL:             types.StringValue(recordStack.APIURL),
		Apikey:             types.StringValue(recordStack.Apikey),
		AppID:              types.StringValue(recordStack.AppID),
		CPUAllocationRatio: types.Float64Value(recordStack.CPUAllocationRatio),
		Description:        types.StringValue(recordStack.Description),
		Descr:              types.StringValue(recordStack.Descr),
		Drivers:            flattens.FlattenSimpleTypeToList(ctx, types.StringType, recordStack.Drivers),
		Eco:                types.StringValue(flattenEco(recordStack.Eco)),
		Error:              types.Int64Value(int64(recordStack.Error)),
		GID:                types.Int64Value(int64(recordStack.GID)),
		GUID:               types.Int64Value(int64(recordStack.GUID)),
		Images:             flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordStack.Images),
		Login:              types.StringValue(recordStack.Login),
		MemAllocationRatio: types.Float64Value(recordStack.MemAllocationRatio),
		Name:               types.StringValue(recordStack.Name),
		Password:           types.StringValue(recordStack.Password),
		ReferenceID:        types.StringValue(recordStack.ReferenceID),
		Status:             types.StringValue(recordStack.Status),
		Type:               types.StringValue(recordStack.Type),
	}

	packages := models.PackagesModel{}
	packages.LibvirtBin = &models.LibvirtBinModel{
		InstalledSize: types.StringValue(recordStack.Packages.LibvirtBin.InstalledSize),
		Ver:           types.StringValue(recordStack.Packages.LibvirtBin.Ver),
	}

	packages.LibvirtDaemon = &models.LibvirtDaemonModel{
		InstalledSize: types.StringValue(recordStack.Packages.LibvirtDaemon.InstalledSize),
		Ver:           types.StringValue(recordStack.Packages.LibvirtDaemon.Ver),
	}

	packages.Lvm2Lockd = &models.Lvm2LockdModel{
		InstalledSize: types.StringValue(recordStack.Packages.Lvm2Lockd.InstalledSize),
		Ver:           types.StringValue(recordStack.Packages.Lvm2Lockd.Ver),
	}

	packages.OpenvswitchCommon = &models.OpenvswitchCommonModel{
		InstalledSize: types.StringValue(recordStack.Packages.OpenvswitchCommon.InstalledSize),
		Ver:           types.StringValue(recordStack.Packages.OpenvswitchCommon.Ver),
	}

	packages.OpenvswitchSwitch = &models.OpenvswitchSwitchModel{
		InstalledSize: types.StringValue(recordStack.Packages.OpenvswitchSwitch.InstalledSize),
		Ver:           types.StringValue(recordStack.Packages.OpenvswitchSwitch.Ver),
	}

	packages.QemuSystemX86 = &models.QemuSystemX86Model{
		InstalledSize: types.StringValue(recordStack.Packages.QemuSystemX86.InstalledSize),
		Ver:           types.StringValue(recordStack.Packages.QemuSystemX86.Ver),
	}

	packages.Sanlock = &models.SanlockModel{
		InstalledSize: types.StringValue(recordStack.Packages.Sanlock.InstalledSize),
		Ver:           types.StringValue(recordStack.Packages.Sanlock.Ver),
	}

	state.Packages = &packages

	tflog.Info(ctx, "flattens.StackDataSource: after flatten", map[string]any{"stack_id": state.StackID.ValueInt64()})

	tflog.Info(ctx, "End flattens.StackDataSource", map[string]any{"stack_id": state.StackID.ValueInt64()})
	return nil
}
