package logicalports

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

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

// MigrateStartRequest struct to start migrate
type MigrateStartRequest 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"`

	// Hypervisor
	// Required: true
	TargetHypervisor string `url:"target_hypervisor" json:"target_hypervisor" validate:"required"`
}

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

	url := "/sdn/logical_port/migration_start"

	res, err := i.client.DecortApiCallCtype(ctx, http.MethodPost, 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
}
