import pathlib
import typing as t

import click

import mantik.cli.runs._arguments as _arguments
import mantik.cli.runs._options as _options
import mantik.cli.runs.runs as runs
import mantik.unicore as unicore


@runs.cli.command("cancel")
@_arguments.JOB_ID
@_options.API_URL
@_options.BACKEND_CONFIG_ABSOLUTE
def cancel_job(
    job_id: str,
    api_url: t.Optional[str],
    backend_config: t.Optional[pathlib.Path],
) -> None:
    """Cancel a submitted run.

    JOB_ID is the job ID assigned by UNICORE.

    """
    client = unicore.client.Client.from_api_url_or_config(
        api_url=api_url, config=backend_config
    )
    job = client.get_job(job_id)
    job.cancel()
    click.echo(f"Cancelled job {job_id}.")
