"""Shared utilities for CLI command modules."""

from __future__ import annotations

import functools

import click
from rich.console import Console

from ..config import Config
from ..db import Database

console = Console()


def _get_config() -> Config:
    cfg = Config.load()
    return cfg


def pass_cfg_db(f):
    """Decorator that extracts cfg and db from Click context and passes them as arguments.

    Usage: place @pass_cfg_db after all @click decorators. The decorated function
    should accept (cfg, db, ...) instead of manually calling _get_config()/Database().
    """

    @click.pass_context
    @functools.wraps(f)
    def wrapper(ctx, **kwargs):
        return f(ctx.obj["cfg"], ctx.obj["db"], **kwargs)
    return wrapper
