"""Session and handle ID generation helpers.

Provides thread-safe sequential ID generation for OutputSession and
ElementHandle instances.  Extracted from ``session.py`` for file-length
compliance (R2-C2 fix).

P3-1 fix (Luis review): session and handle IDs now use separate
counters so that IDs are monotonic within their own namespace.
"""

from __future__ import annotations

import threading

_SESSION_LOCK = threading.Lock()
_SESSION_COUNTER = 0

_HANDLE_LOCK = threading.Lock()
_HANDLE_COUNTER = 0


def generate_session_id() -> str:
    """Generate a unique session identifier."""
    global _SESSION_COUNTER
    with _SESSION_LOCK:
        _SESSION_COUNTER += 1
        return f"ses-{_SESSION_COUNTER:06d}"


def generate_handle_id() -> str:
    """Generate a unique handle identifier."""
    global _HANDLE_COUNTER
    with _HANDLE_LOCK:
        _HANDLE_COUNTER += 1
        return f"hdl-{_HANDLE_COUNTER:06d}"


def _reset_counters() -> None:
    """Reset both counters to zero (test helper only)."""
    global _SESSION_COUNTER, _HANDLE_COUNTER
    with _SESSION_LOCK:
        _SESSION_COUNTER = 0
    with _HANDLE_LOCK:
        _HANDLE_COUNTER = 0
