package backup

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

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

// DeleteDiskBackupRequest struct for deleting disk backup
type DeleteDiskBackupRequest struct {
	// Backup path
	BackupPath string `url:"backupPath" json:"backupPath" validate:"required"`

	// Backup file
	BackupFile string `url:"backupFile" json:"backupFile" validate:"required"`
}

type wrapperDeleteDiskBackupRequest struct {
	DeleteDiskBackupRequest

	AsyncMode bool `url:"asyncMode"`
}

// DeleteDiskBackup deletes disk backup
func (b Backup) DeleteDiskBackup(ctx context.Context, req DeleteDiskBackupRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperDeleteDiskBackupRequest{
		DeleteDiskBackupRequest: req,
		AsyncMode:               false,
	}

	url := "/cloudbroker/backup/deleteDiskBackup"

	res, err := b.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
}

// DeleteDiskBackupAsync deletes disk backup
func (b Backup) DeleteDiskBackupAsync(ctx context.Context, req DeleteDiskBackupRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperDeleteDiskBackupRequest{
		DeleteDiskBackupRequest: req,
		AsyncMode:               true,
	}

	url := "/cloudbroker/backup/deleteDiskBackup"

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

	result := strings.ReplaceAll(string(res), "\"", "")

	return result, nil
}
