package flattens

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

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

	diags := diag.Diagnostics{}

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

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

	*plan = models.ResourceFLIPGroupModel{
		AccountID:   types.Int64Value(int64(recordFG.AccountID)),
		Name:        types.StringValue(recordFG.Name),
		NetType:     types.StringValue(recordFG.NetType),
		NetID:       types.Int64Value(int64(recordFG.NetID)),
		ClientType:  types.StringValue(recordFG.ClientType),
		Timeouts:    plan.Timeouts,
		Description: types.StringValue(recordFG.Description),
		ClientIDs:   flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordFG.ClientIDs),
		ID:          plan.ID,
		IP:          types.StringValue(recordFG.IP),

		AccountName: types.StringValue(recordFG.AccountName),
		ConnID:      types.Int64Value(int64(recordFG.ConnID)),
		ConnType:    types.StringValue(recordFG.ConnType),
		CreatedBy:   types.StringValue(recordFG.CreatedBy),
		CreatedTime: types.Int64Value(int64(recordFG.CreatedTime)),
		DefaultGW:   types.StringValue(recordFG.DefaultGW),
		DeletedBy:   types.StringValue(recordFG.DeletedBy),
		DeletedTime: types.Int64Value(int64(recordFG.DeletedTime)),
		FlipgroupID: types.Int64Value(int64(recordFG.ID)),
		//ID:          types.StringValue(strconv.Itoa(int(recordFG.ID))),
		GID:         types.Int64Value(int64(recordFG.GID)),
		GUID:        types.Int64Value(int64(recordFG.GUID)),
		Milestones:  types.Int64Value(int64(recordFG.Milestones)),
		Network:     types.StringValue(recordFG.Network),
		RGID:        types.Int64Value(int64(recordFG.RGID)),
		RGName:      types.StringValue(recordFG.RGName),
		Status:      types.StringValue(recordFG.Status),
		UpdatedBy:   types.StringValue(recordFG.UpdatedBy),
		UpdatedTime: types.Int64Value(int64(recordFG.UpdatedTime)),
	}

	if plan.IP.IsUnknown() {
		plan.IP = types.StringValue(recordFG.IP)
	}

	tflog.Info(ctx, "End flattens.FlipgroupResource", map[string]any{"flipgroup_id": plan.ID.ValueString()})
	return nil
}
