# app/core/environment.py
"""
Environment detection utilities.

Automatically detects environment based on runtime conditions
rather than relying on configuration.
"""

import os
from typing import Literal

EnvironmentType = Literal["development", "staging", "production"]


def get_environment() -> EnvironmentType:
    """
    Detect current environment automatically.

    Detection logic:
    1. Check ENV environment variable if set
    2. Check ENVIRONMENT environment variable if set
    3. Auto-detect based on hostname/indicators:
       - localhost, 127.0.0.1 → development
       - Contains 'staging' → staging
       - Otherwise → production (safe default)

    Returns:
        str: 'development', 'staging', or 'production'
    """
    # Priority 1: Explicit ENV variable
    env = os.getenv("ENV", "").lower()
    if env in ["development", "dev", "local"]:
        return "development"
    if env in ["staging", "stage"]:
        return "staging"
    if env in ["production", "prod"]:
        return "production"

    # Priority 2: ENVIRONMENT variable
    env = os.getenv("ENVIRONMENT", "").lower()
    if env in ["development", "dev", "local"]:
        return "development"
    if env in ["staging", "stage"]:
        return "staging"
    if env in ["production", "prod"]:
        return "production"

    # Priority 3: Auto-detect from common indicators

    # Check if running in debug mode (common in development)
    if os.getenv("DEBUG", "").lower() in ["true", "1", "yes"]:
        return "development"

    # Check common development indicators
    hostname = os.getenv("HOSTNAME", "").lower()
    if any(
        dev_indicator in hostname
        for dev_indicator in ["local", "dev", "laptop", "desktop"]
    ):
        return "development"

    # Check for staging indicators
    if "staging" in hostname or "stage" in hostname:
        return "staging"

    # Default to development for safety (HTTPS not required in dev)
    # Change this to "production" if you prefer secure-by-default
    return "development"


def is_development() -> bool:
    """Check if running in development environment."""
    return get_environment() == "development"


def is_staging() -> bool:
    """Check if running in staging environment."""
    return get_environment() == "staging"


def is_production() -> bool:
    """Check if running in production environment."""
    return get_environment() == "production"


def should_use_secure_cookies() -> bool:
    """
    Determine if cookies should have secure flag (HTTPS only).

    Returns:
        bool: True if production or staging, False if development
    """
    return not is_development()


# Cache the environment detection result
_cached_environment: EnvironmentType | None = None


def get_cached_environment() -> EnvironmentType:
    """
    Get environment with caching.

    Environment is detected once and cached for performance.
    Useful if you call this frequently.
    """
    global _cached_environment
    if _cached_environment is None:
        _cached_environment = get_environment()
    return _cached_environment
