"""abreka wiki merge — fold one wiki page into another."""

from __future__ import annotations

import click

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


@click.command("merge")
@click.argument("src_slug")
@click.argument("dst_slug")
@click.option("--exp", "exp_id", default="", help="Experiment id recording this merge.")
def merge_cmd(src_slug: str, dst_slug: str, exp_id: str):
    """Append SRC's body to DST and delete SRC. Tags are unioned."""
    abreka_dir = find_abreka_dir()
    try:
        result = merge_pages(abreka_dir, src_slug, dst_slug, exp_id=exp_id)
    except WikiPageNotFoundError as exc:
        raise click.ClickException(str(exc))
    except WikiError as exc:
        raise click.ClickException(str(exc))
    click.echo(f"Merged {src_slug} → {result.slug}")
