package utilities

import (
	"context"

	"github.com/hashicorp/terraform-plugin-framework/diag"
	"github.com/hashicorp/terraform-plugin-log/tflog"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/disks"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/client"
	"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/models"
)

func DataSourceDiskListTypesCheckPresence(ctx context.Context, state *models.DataSourceDiskListTypesModel, c *client.Client) (*disks.ListTypes, diag.Diagnostics) {
	diags := diag.Diagnostics{}

	listTypesReq := disks.ListTypesRequest{Detailed: false}

	if !state.SortBy.IsNull() {
		listTypesReq.SortBy = state.SortBy.ValueString()
	}
	if !state.Page.IsNull() {
		listTypesReq.Page = uint64(state.Page.ValueInt64())
	}
	if !state.Size.IsNull() {
		listTypesReq.Size = uint64(state.Size.ValueInt64())
	}

	tflog.Info(ctx, "DataSourceDiskListTypesCheckPresence: before call CloudAPI().Disks().ListTypes", map[string]any{
		"req": listTypesReq,
	})

	listTypes, err := c.CloudAPI().Disks().ListTypes(ctx, listTypesReq)
	if err != nil {
		diags.AddError("Cannot get info about disk list types", err.Error())
		return nil, diags
	}

	tflog.Info(ctx, "DataSourceDiskListTypesCheckPresence: got list successfully", map[string]any{
		"entry_count": listTypes.EntryCount,
	})
	return listTypes, nil
}
