"""Foreground (topmost) window/process API."""

import asyncio
import logging

from fastapi import APIRouter, Depends

from ..auth import verify_token
from ..services.foreground_service import get_foreground_info

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/api/foreground", tags=["foreground"])


@router.get("")
async def get_foreground(
    refresh: bool = False, _: str = Depends(verify_token)
) -> dict:
    """Return metadata about the foreground window and owning process.

    The probe is cached for ~500ms server-side; pass ``?refresh=1`` to bypass
    the cache for one-shot queries.
    """
    info = await asyncio.to_thread(get_foreground_info, refresh)
    return info.to_dict()
