package flattens

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

func ResourceImageVirtual(ctx context.Context, plan *models.ImageVirtualResourceModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start FlattenResourceImageVirtual", map[string]any{"image_id": plan.ImageId.ValueInt64()})

	diags := diag.Diagnostics{}

	imageId, err := strconv.ParseUint(plan.Id.ValueString(), 10, 64)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot parsed ID image from state %d", imageId), err.Error())
		return diags
	}

	image, err := utilities.ImageResourceCheckPresence(ctx, imageId, c)
	if err != nil {
		diags.AddError(fmt.Sprintf("Cannot get info about image with ID %d", plan.ImageId.ValueInt64()), err.Error())
		return diags
	}

	cdPresentedTo, _ := json.Marshal(image.CdPresentedTo)
	*plan = models.ImageVirtualResourceModel{
		ImageName:              types.StringValue(image.Name),
		LinkTo:                 types.Int64Value(int64(image.LinkTo)),
		Timeouts:               plan.Timeouts,
		Id:                     types.StringValue(strconv.Itoa(int(image.ID))),
		LastUpdated:            plan.LastUpdated,
		UNCPath:                types.StringValue(image.UNCPath),
		CKey:                   types.StringValue(image.CKey),
		AccountID:              types.Int64Value(int64(image.AccountID)),
		Architecture:           types.StringValue(image.Architecture),
		BootType:               types.StringValue(image.BootType),
		Bootable:               types.BoolValue(image.Bootable),
		CdPresentedTo:          types.StringValue(string(cdPresentedTo)),
		ComputeCIID:            types.Int64Value(int64(image.ComputeCIID)),
		DeletedTime:            types.Int64Value(int64(image.DeletedTime)),
		Description:            types.StringValue(image.Description),
		Enabled:                types.BoolValue(image.Enabled),
		GID:                    types.Int64Value(int64(image.GID)),
		GUID:                   types.Int64Value(int64(image.GUID)),
		History:                flattenHistoryInResource(ctx, &image.History),
		HotResize:              types.BoolValue(image.HotResize),
		LastModified:           types.Int64Value(int64(image.LastModified)),
		Milestones:             types.Int64Value(int64(image.Milestones)),
		NetworkInterfaceNaming: types.StringValue(image.NetworkInterfaceNaming),
		ImageId:                types.Int64Value(int64(image.ID)),
		ImageType:              types.StringValue(image.Type),
		Password:               types.StringValue(image.Password),
		PoolName:               types.StringValue(image.Pool),
		ProviderName:           types.StringValue(image.ProviderName),
		PurgeAttempts:          types.Int64Value(int64(image.PurgeAttempts)),
		ResID:                  types.StringValue(image.ResID),
		RescueCD:               types.BoolValue(image.RescueCD),
		SepID:                  types.Int64Value(int64(image.SepID)),
		Size:                   types.Int64Value(int64(image.Size)),
		SnapshotID:             types.StringValue(image.SnapshotID),
		Status:                 types.StringValue(image.Status),
		TechStatus:             types.StringValue(image.TechStatus),
		Username:               types.StringValue(image.Username),
		Version:                types.StringValue(image.Version),
	}

	plan.ACL, diags = types.ListValueFrom(ctx, types.StringType, image.ACL)
	if diags != nil {
		tflog.Error(ctx, fmt.Sprint("Error flattenACL", diags))
	}
	plan.Drivers, diags = types.ListValueFrom(ctx, types.StringType, image.Drivers)
	if diags != nil {
		tflog.Error(ctx, fmt.Sprint("Error flattenDrivers", diags))
	}
	plan.PresentTo, diags = types.MapValueFrom(ctx, types.Int64Type, image.PresentTo)
	if diags != nil {
		tflog.Error(ctx, fmt.Sprint("Error flattenPresentTo", diags))
	}
	plan.SharedWith, diags = types.ListValueFrom(ctx, types.Int64Type, image.SharedWith)
	if diags != nil {
		tflog.Error(ctx, fmt.Sprint("Error flattenSharedWith", diags))
	}

	tflog.Info(ctx, "End FlattenResourceImageVirtual", map[string]any{"image_id": plan.ImageId.ValueInt64()})
	return nil
}
