"""Environment-specific configuration and utilities."""

from __future__ import annotations

import logging
import os
import sys
from typing import TYPE_CHECKING

from dotenv import load_dotenv

if TYPE_CHECKING:
    from collections.abc import Callable


IS_PRODUCTION = os.environ.get("ENVIRONMENT", default="") == "PRODUCTION"

LOGGING_FORMAT = "[%(levelname)s] %(message)s"

logging.getLogger("parso").setLevel(logging.WARNING)


def load_env() -> None:
    """Load environment variables from .env file in non-production."""
    if not IS_PRODUCTION:
        load_dotenv()


def set_logging_level() -> None:
    """Set logging level based on environment."""
    if IS_PRODUCTION:
        logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT, stream=sys.stdout)
    else:
        logging.basicConfig(level=logging.DEBUG, format=LOGGING_FORMAT, stream=sys.stdout)


def debug_log_sensitive_data(message: str) -> None:
    """Log sensitive data only in non-production environments."""
    if not IS_PRODUCTION:
        logging.debug(message)


def debug_log_sensitive_data_from_callable(message: Callable[[], str | None]) -> None:
    """Log sensitive data from callable only in non-production environments."""
    if not IS_PRODUCTION:
        logging.debug(message())
