"""abreka wiki tags — show all tags with page counts."""

from __future__ import annotations

import click
from rich.console import Console
from rich.table import Table

from abreka.config import find_abreka_dir
from abreka.wiki import collect_tags


@click.command("tags")
def tags_cmd():
    """Show wiki tags sorted by frequency."""
    abreka_dir = find_abreka_dir()
    counts = collect_tags(abreka_dir)
    if not counts:
        click.echo("No tags.")
        return

    console = Console()
    table = Table(show_header=True, header_style="bold")
    table.add_column("Tag", style="bold")
    table.add_column("Pages", justify="right")
    for tag, n in sorted(counts.items(), key=lambda kv: (-kv[1], kv[0])):
        table.add_row(tag, str(n))
    console.print(table)
