"""abreka wiki show — display a wiki page."""

from __future__ import annotations

import click

from abreka.config import find_abreka_dir
from abreka.wiki import (
    WikiPageNotFoundError,
    load_wiki_page,
    serialize_page,
)


@click.command("show")
@click.argument("slug")
@click.option("--raw", is_flag=True, help="Emit the raw on-disk markdown (with frontmatter).")
def show_cmd(slug: str, raw: bool):
    """Show a wiki page's body (and metadata)."""
    abreka_dir = find_abreka_dir()
    try:
        page = load_wiki_page(abreka_dir, slug)
    except WikiPageNotFoundError as exc:
        raise click.ClickException(str(exc))

    if raw:
        click.echo(serialize_page(page), nl=False)
        return

    click.echo(f"# {page.title}")
    click.echo(f"slug: {page.slug}")
    if page.tags:
        click.echo(f"tags: {', '.join(page.tags)}")
    if page.summary:
        click.echo(f"summary: {page.summary}")
    click.echo(f"updated: {page.updated_at.isoformat()}")
    if page.updated_by_exp:
        click.echo(f"updated_by_exp: {page.updated_by_exp}")
    click.echo()
    click.echo(page.body.rstrip())
