package utilities

import (
	"context"
	"fmt"

	"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"
)

func ImageDataSourceCheckPresence(ctx context.Context, state *models.RecordImageModel, c *client.Client) (*image.RecordImage, error) {
	tflog.Info(ctx, "Get info about image with ID", map[string]any{"image_id": state.ImageId.ValueInt64()})

	req := image.GetRequest{ImageID: uint64(state.ImageId.ValueInt64())}

	if state.ShowAll.IsNull() {
		req.ShowAll = false
	} else {
		req.ShowAll = state.ShowAll.ValueBool()
	}

	image, err := c.CloudAPI().Image().Get(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("cannot get info about image with error: %w", err)
	}

	tflog.Info(ctx, "Getting info about image successfully", map[string]any{"image_id": state.ImageId.ValueInt64()})
	return image, nil
}
