"""
Job type registry for the /ui/jobs/ page.

To add a new job tab:
  1. Call ``register(job_type_dict)`` from ``LinksConfig.ready()`` in links/apps.py.
  2. Add the corresponding bulk action handler in ``JobsView.post()``.

Each job type dict must have:
  id (str)                  unique tab slug, used as ?tab= value
  label (str)               display name shown in the tab bar
  icon_color (str)          Tailwind text-colour class, e.g. 'text-indigo-500'
  icon_path (str)           SVG <path d="..."> value for the stats card icon
  title_label (str)         column header for the title/name column
  status_choices (list)     [(value, label), ...]; first entry should be ('all', 'All')
  columns (list[str])       ordered column ids from:
                            ['id','title','source_url','status','retry','error','size','updated']
  get_stats (callable)      () -> dict with 'total' plus any of:
                            pending / processing / completed / failed
                            (omit keys that do not apply to this type)
  get_queryset (callable)   (status_filter: str) -> QuerySet
  serialize (callable)      (obj) -> dict with keys:
                              id, title, detail_url, status,
                              retry (int|None), retry_max (int|None),
                              error (str), updated_at (datetime),
                              extra (dict – for source_url, formatted_size, etc.)
  bulk_actions (dict)       subset of {'retry': action_name,
                                        'fail':  action_name,
                                        'delete': action_name}
"""
import logging

logger = logging.getLogger(__name__)

_registry: list[dict] = []


def register(job_type: dict) -> None:
    """Register a job type. Call from LinksConfig.ready()."""
    _registry.append(job_type)
    logger.debug("Registered job type: %s", job_type.get("id"))


def all_types() -> list[dict]:
    """Return all registered job types in registration order."""
    return list(_registry)


def get_type(type_id: str) -> dict | None:
    """Return the job type dict with the given id, or None."""
    return next((j for j in _registry if j["id"] == type_id), None)
