package logicalports

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

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

// MigrateCancelRequest struct to cancel migrate
type MigrateCancelRequest struct {
	// ID of the logical port
	// Required: true
	LogicalPortID string `url:"logical_port_id" json:"logical_port_id" validate:"required"`

	// ID of the version
	// Required: true
	VersionID uint64 `url:"version_id" json:"version_id" validate:"required"`
}

func (i LogicalPorts) CancelMigrate(ctx context.Context, req MigrateCancelRequest) (*MigrationStatus, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/logical_port/migration_cancel"

	res, err := i.client.DecortApiCallCtype(ctx, http.MethodDelete, url, constants.MIMEJSON, req)
	if err != nil {
		return nil, err
	}

	info := MigrationStatus{}

	err = json.Unmarshal(res, &info)
	if err != nil {
		return nil, err
	}

	return &info, nil
}
