"""
Recording API - In-Memory Storage & Helpers.

Shared state and utility functions for recording endpoints.
"""

import uuid
from datetime import datetime
from typing import Optional


# ==========================================
# IN-MEMORY STORAGE (Dev Mode)
# ==========================================
# In production, these would be database queries

_recordings_store: dict = {}
_transcriptions_store: dict = {}
_audit_log: list = []
_minutes_store: dict = {}


def log_audit(
    action: str,
    recording_id: Optional[str] = None,
    transcription_id: Optional[str] = None,
    user_id: Optional[str] = None,
    metadata: Optional[dict] = None
):
    """Log audit event for DSGVO compliance."""
    _audit_log.append({
        "id": str(uuid.uuid4()),
        "recording_id": recording_id,
        "transcription_id": transcription_id,
        "user_id": user_id,
        "action": action,
        "metadata": metadata or {},
        "created_at": datetime.utcnow().isoformat()
    })


def format_vtt_time(ms: int) -> str:
    """Format milliseconds to VTT timestamp (HH:MM:SS.mmm)."""
    hours = ms // 3600000
    minutes = (ms % 3600000) // 60000
    seconds = (ms % 60000) // 1000
    millis = ms % 1000
    return f"{hours:02d}:{minutes:02d}:{seconds:02d}.{millis:03d}"


def format_srt_time(ms: int) -> str:
    """Format milliseconds to SRT timestamp (HH:MM:SS,mmm)."""
    hours = ms // 3600000
    minutes = (ms % 3600000) // 60000
    seconds = (ms % 60000) // 1000
    millis = ms % 1000
    return f"{hours:02d}:{minutes:02d}:{seconds:02d},{millis:03d}"
