"""abreka wiki rename — rename a wiki page's slug."""

from __future__ import annotations

import click

from abreka.config import find_abreka_dir
from abreka.wiki import (
    WikiError,
    WikiPageExistsError,
    WikiPageNotFoundError,
    rename_page,
)


@click.command("rename")
@click.argument("old_slug")
@click.argument("new_slug")
@click.option("--exp", "exp_id", default="", help="Experiment id recording this rename.")
def rename_cmd(old_slug: str, new_slug: str, exp_id: str):
    """Rename a wiki page's slug."""
    abreka_dir = find_abreka_dir()
    try:
        page = rename_page(abreka_dir, old_slug, new_slug, exp_id=exp_id)
    except WikiPageNotFoundError as exc:
        raise click.ClickException(str(exc))
    except WikiPageExistsError as exc:
        raise click.ClickException(str(exc))
    except WikiError as exc:
        raise click.ClickException(str(exc))
    click.echo(page.slug)
