package compute

import (
	"context"
	"net/http"
	"strconv"

	"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)

// MigrateStorageCleanUpRequest struct to cleanup resources after finished migration
type MigrateStorageCleanUpRequest struct {
	// ID of the compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`
}

type wrapperMigrateStorageCleanUpRequest struct {
	MigrateStorageCleanUpRequest

	AsyncMode bool `url:"asyncMode"`
}

// MigrateStorageCleanUp cleanup resources after finished (success of failed) complex compute migration.
// If the migration was successful, then old disks will be removed, else new (target) disks will be removed.
// Do it wisely!
func (c Compute) MigrateStorageCleanUp(ctx context.Context, req MigrateStorageCleanUpRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperMigrateStorageCleanUpRequest{
		MigrateStorageCleanUpRequest: req,
		AsyncMode:                    false,
	}

	url := "/cloudbroker/compute/migrateStorageCleanup"

	res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped)
	if err != nil {
		return false, err
	}

	result, err := strconv.ParseBool(string(res))
	if err != nil {
		return false, err
	}

	return result, nil
}

// MigrateStorageCleanUpAsync cleanup resources after finished migration with AsyncMode
func (c Compute) MigrateStorageCleanUpAsync(ctx context.Context, req MigrateStorageCleanUpRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperMigrateStorageCleanUpRequest{
		MigrateStorageCleanUpRequest: req,
		AsyncMode:                    true,
	}

	url := "/cloudbroker/compute/migrateStorageCleanup"

	res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped)
	if err != nil {
		return "", err
	}

	return string(res), nil
}
