package disks

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

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

// ReplicationResume struct to resume suspended replication
type ReplicationResumeRequest struct {
	// Id of the disk
	// Required: true
	DiskID uint64 `url:"diskId" json:"diskId" validate:"required"`
}

type wrapperReplicationResumeRequest struct {
	ReplicationResumeRequest

	AsyncMode bool `url:"async_mode" json:"async_mode"`
}

// ReplicationResume resume suspended replication
func (d Disks) ReplicationResume(ctx context.Context, req ReplicationResumeRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/disks/replicationResume"

	wrappedReq := wrapperReplicationResumeRequest{
		ReplicationResumeRequest: req,
		AsyncMode:                false,
	}

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

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

	return result, nil
}

// ReplicationResumeAsync resumes suspended replication in async mode
func (d Disks) ReplicationResumeAsync(ctx context.Context, req ReplicationResumeRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/disks/replicationResume"

	wrappedReq := wrapperReplicationResumeRequest{
		ReplicationResumeRequest: req,
		AsyncMode:                true,
	}

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

	var result string
	if err := json.Unmarshal(res, &result); err != nil {
		return "", err
	}

	return result, nil
}
