"""
Version management for BLOOM LIMS.

Version resolution priority:
1. BLOOM_VERSION environment variable (override / CI)
2. importlib.metadata  (installed package)
3. bloom_lims._scm_version  (generated by setuptools-scm at build time)
4. Hard fallback "0.0.0.dev0"

_scm_version.py is auto-generated — do not edit or commit it.
"""

from __future__ import annotations

import os
from functools import lru_cache


@lru_cache(maxsize=1)
def get_version() -> str:
    """Return the BLOOM LIMS version string."""
    env_version = os.environ.get("BLOOM_VERSION")
    if env_version:
        return env_version

    try:
        from importlib.metadata import version
        return version("bloom_lims")
    except Exception:
        pass

    try:
        from bloom_lims._scm_version import __version__ as scm_version
        return scm_version
    except ImportError:
        pass

    return "0.0.0.dev0"


def clear_version_cache() -> None:
    """Clear the cached version (useful for testing)."""
    get_version.cache_clear()


# Module-level version for easy import
__version__ = get_version()
