package flattens

import (
	"context"
	"fmt"
	"strconv"

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

// RGResource flattens resource for rg (resource group).
// Return error in case resource is not found on the platform.
// Flatten errors are added to tflog.
func RGResource(ctx context.Context, plan *models.ResourceRGModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start flattens.RGResource")

	diags := diag.Diagnostics{}

	rgId, err := strconv.ParseUint(plan.Id.ValueString(), 10, 64)
	if err != nil {
		diags.AddError("Cannot parse resource group ID from state", err.Error())
		return diags
	}

	recordRG, err := utilities.RGCheckPresence(ctx, rgId, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about resource group with ID %v", rgId), err.Error())
		return diags
	}

	tflog.Info(ctx, "flattens.RGResource: before flatten", map[string]any{"rg_id": rgId, "recordRG": recordRG})

	*plan = models.ResourceRGModel{
		AccountID: types.Int64Value(int64(recordRG.AccountID)),
		GID:       types.Int64Value(int64(recordRG.GID)),
		Name:      types.StringValue(recordRG.Name),

		DefNetType:       plan.DefNetType,
		IPCIDR:           plan.IPCIDR,
		ResourceLimits:   flattenResourceLimits(ctx, &recordRG.ResourceLimits),
		ComputeFeatures:  plan.ComputeFeatures,
		ExtNetID:         plan.ExtNetID,
		ExtIP:            plan.ExtIP,
		Owner:            plan.Owner,
		Access:           plan.Access,
		DefNet:           plan.DefNet,
		Description:      plan.Description,
		Force:            plan.Force,
		Permanently:      plan.Permanently,
		RegisterComputes: plan.RegisterComputes,
		Restore:          plan.Restore,
		Enable:           plan.Enable,
		UniqPools:        plan.UniqPools,
		Timeouts:         plan.Timeouts,

		RGID:                   types.Int64Value(int64(recordRG.ID)),
		LastUpdated:            plan.LastUpdated,
		AccountName:            types.StringValue(recordRG.AccountName),
		ACL:                    flattenACL(ctx, &recordRG.ACL),
		CPUAllocationParameter: types.StringValue(recordRG.CPUAllocationParameter),
		CPUAllocationRatio:     types.Float64Value(recordRG.CPUAllocationRatio),
		DefNetID:               types.Int64Value(recordRG.DefNetID),
		DeletedBy:              types.StringValue(recordRG.DeletedBy),
		DeletedTime:            types.Int64Value(int64(recordRG.DeletedTime)),
		GUID:                   types.Int64Value(int64(recordRG.GUID)),
		Id:                     types.StringValue(strconv.Itoa(int(recordRG.ID))),
		LockStatus:             types.StringValue(recordRG.LockStatus),
		Milestones:             types.Int64Value(int64(recordRG.Milestones)),
		Secret:                 types.StringValue(recordRG.Secret),
		Status:                 types.StringValue(recordRG.Status),
		UpdatedBy:              types.StringValue(recordRG.UpdatedBy),
		UpdatedTime:            types.Int64Value(int64(recordRG.UpdatedTime)),
		ResourceTypes:          flattens.FlattenSimpleTypeToList(ctx, types.StringType, &recordRG.ResTypes),
		VINS:                   flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, &recordRG.VINS),
		VMS:                    flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, &recordRG.VMs),
	}

	tflog.Info(ctx, "flattenResourceRG: after flatten", map[string]any{"rg_id": plan.Id.ValueString()})

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