package utilities

import (
	"context"

	"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 ImageListCheckPresence(ctx context.Context, state *models.ListImagesModel, c *client.Client) (*image.ListImages, error) {
	tflog.Info(ctx, "Get image list info")

	req := image.ListRequest{}

	if !state.SEPID.IsNull() {
		req.SEPID = uint64(state.SEPID.ValueInt64())
	}
	if !state.ByID.IsNull() {
		req.ByID = uint64(state.ByID.ValueInt64())
	}
	if !state.Name.IsNull() {
		req.Name = state.Name.ValueString()
	}
	if !state.Status.IsNull() {
		req.Status = state.Status.ValueString()
	}
	if !state.Architecture.IsNull() {
		req.Architecture = state.Architecture.ValueString()
	}
	if !state.TypeImage.IsNull() {
		req.TypeImage = state.TypeImage.ValueString()
	}
	if !state.ImageSize.IsNull() {
		req.ImageSize = uint64(state.ImageSize.ValueInt64())
	}
	if !state.SEPName.IsNull() {
		req.SEPName = state.SEPName.ValueString()
	}
	if !state.PoolName.IsNull() {
		req.Pool = state.PoolName.ValueString()
	}
	if !state.Public.IsNull() {
		req.Public = state.Public.ValueBool()
	}
	if !state.HotResize.IsNull() {
		req.HotResize = state.HotResize.ValueBool()
	}
	if !state.Bootable.IsNull() {
		req.Bootable = state.Bootable.ValueBool()
	}
	if !state.SortBy.IsNull() {
		req.SortBy = state.SortBy.ValueString()
	}
	if !state.Size.IsNull() {
		req.Size = uint64(state.Size.ValueInt64())
	}
	if !state.Enabled.IsNull() {
		req.Enabled = state.Enabled.ValueBool()
	}
	if !state.Page.IsNull() {
		req.Page = uint64(state.Page.ValueInt64())
	}

	imageList, err := c.CloudAPI().Image().List(ctx, req)
	if err != nil {
		return nil, err
	}

	tflog.Info(ctx, "Getting image list info, successfully")
	return imageList, nil
}
