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/cloudapi/image"
	"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 DataSourceImageList(ctx context.Context, state *models.ListImagesModel, c *client.Client) diag.Diagnostics {
	tflog.Info(ctx, "Start FlattenDataSourceImageList")

	diags := diag.Diagnostics{}

	imageList, err := utilities.ImageListCheckPresence(ctx, state, c)
	if err != nil {
		diags.AddError("Error get image list info", err.Error())
		return diags
	}

	id := uuid.New()

	*state = models.ListImagesModel{
		SEPID:        state.SEPID,
		ByID:         state.ByID,
		Name:         state.Name,
		Status:       state.Status,
		Architecture: state.Architecture,
		TypeImage:    state.TypeImage,
		ImageSize:    state.ImageSize,
		SEPName:      state.SEPName,
		PoolName:     state.PoolName,
		Public:       state.Public,
		HotResize:    state.HotResize,
		Bootable:     state.Bootable,
		SortBy:       state.SortBy,
		Page:         state.Page,
		Enabled:      state.Enabled,
		Size:         state.Size,
		Timeouts:     state.Timeouts,
		Id:           types.StringValue(id.String()),
		Items:        flattenItems(ctx, imageList),
		EntryCount:   types.Int64Value(int64(imageList.EntryCount)),
	}

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

func flattenItems(ctx context.Context, data *image.ListImages) []models.ItemImageModel {
	tflog.Info(ctx, "Start flattenItems")
	var diags diag.Diagnostics

	res := make([]models.ItemImageModel, 0, len(data.Data))
	for _, item := range data.Data {
		temp := models.ItemImageModel{
			AccountID:              types.Int64Value(int64(item.AccountID)),
			Architecture:           types.StringValue(item.Architecture),
			BootType:               types.StringValue(item.BootType),
			Bootable:               types.BoolValue(item.Bootable),
			CDROM:                  types.BoolValue(item.CDROM),
			Description:            types.StringValue(item.Description),
			HotResize:              types.BoolValue(item.HotResize),
			ImageID:                types.Int64Value(int64(item.ID)),
			ImageType:              types.StringValue(item.Type),
			LinkTo:                 types.Int64Value(int64(item.LinkTo)),
			ImageName:              types.StringValue(item.Name),
			PoolName:               types.StringValue(item.Pool),
			SepID:                  types.Int64Value(int64(item.SepID)),
			Size:                   types.Int64Value(int64(item.Size)),
			Status:                 types.StringValue(item.Status),
			NetworkInterfaceNaming: types.StringValue(item.NetworkInterfaceNaming),
			Username:               types.StringValue(item.Username),
			Virtual:                types.BoolValue(item.Virtual),
		}
		temp.Drivers, diags = types.ListValueFrom(ctx, types.StringType, item.Drivers)
		if diags != nil {
			tflog.Error(ctx, fmt.Sprint("Error flattenDrivers", diags))
		}
		res = append(res, temp)
	}

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