package service

import (
	"context"

	iop "iop/proto/gen/iop"
)

type CancelRunRequest struct {
	NodeRef string
	RunID   string
}

func BuildCancelRunRequest(req CancelRunRequest) *iop.CancelRequest {
	return &iop.CancelRequest{
		RunId: req.RunID,
	}
}

func (s *Service) CancelRun(_ context.Context, req CancelRunRequest) (CommandResult, error) {
	entry, err := s.ResolveDispatchReady(req.NodeRef)
	if err != nil {
		return CommandResult{}, err
	}
	cancelReq := BuildCancelRunRequest(req)
	if err := entry.Client.Send(cancelReq); err != nil {
		return CommandResult{}, err
	}
	return CommandResult{
		NodeID:    entry.NodeID,
		NodeLabel: nodeLabel(entry),
	}, nil
}

// CommandResult is the surface-neutral acknowledgement for one-shot node
// commands.
type CommandResult struct {
	NodeID    string
	NodeLabel string
}
