# This is free software for the public good of a permacomputer hosted at
# permacomputer.com, an always-on computer by the people, for the people.
# One which is durable, easy to repair, & distributed like tap water
# for machine learning intelligence.
#
# The permacomputer is community-owned infrastructure optimized around
# four values:
#
#   TRUTH      First principles, math & science, open source code freely distributed
#   FREEDOM    Voluntary partnerships, freedom from tyranny & corporate control
#   HARMONY    Minimal waste, self-renewing systems with diverse thriving connections
#   LOVE       Be yourself without hurting others, cooperation through natural law
#
# This software contributes to that vision by archiving the web, preserving digital knowledge before it disappears.
# Code is seeds to sprout on any abandoned technology.

"""
Live media event queue for real-time SSE streaming.

Pushed to after disk save, SSE endpoint reads from here.
"""

import asyncio
from typing import Dict, Any

# Live media event queue - push here after disk save, SSE reads from here
# Format: {'md5_hash': str, 'media_type': str, 'file_size': int, 'alt_text': str, ...}
LIVE_MEDIA_QUEUE: asyncio.Queue = None  # Initialized lazily


def get_live_queue() -> asyncio.Queue:
    """Get or create the live media queue."""
    global LIVE_MEDIA_QUEUE
    if LIVE_MEDIA_QUEUE is None:
        LIVE_MEDIA_QUEUE = asyncio.Queue(maxsize=1000)
    return LIVE_MEDIA_QUEUE


def emit_live_media(media_info: Dict[str, Any]):
    """Emit media to live feed (non-blocking)."""
    try:
        queue = get_live_queue()
        queue.put_nowait(media_info)
    except asyncio.QueueFull:
        pass  # Drop if queue is full (live feed will catch up from DB)
