"""Periodic reservation sweeper entrypoint."""

from __future__ import annotations

import asyncio
import logging
from typing import Any, Dict

from ...db import AsyncSessionLocal
from ..enrichment_usage_tracker import EnrichmentUsageTracker

logger = logging.getLogger(__name__)


async def sweep_once() -> Dict[str, Any]:
    """Expire abandoned reservations and purge stale idempotency keys."""
    async with AsyncSessionLocal() as session:
        tracker = EnrichmentUsageTracker(session)
        summary = await tracker.sweep_timeouts()
        logger.info("reservation_sweeper_summary %s", summary)
        return summary


def sweep_sync() -> Dict[str, Any]:
    """Synchronous helper for cron/management commands."""
    return asyncio.run(sweep_once())
