import os
from analogai.foundation.common.utils.env_utils import (
    load_envs_in_order,
    parse_bool,
    parse_int,
    resolve_cpu_count,
)

load_envs_in_order()

ENVIRONMENT = os.getenv('ENVIRONMENT', 'development')

if ENVIRONMENT not in ['development', 'production']:
    raise ValueError(f"Environment variable ENVIRONMENT must be one of: development or production")

QUICK_TESTING = parse_bool(os.getenv('QUICK_TESTING', 'false'), False)
FINAL_APPLICATION_TESTING = parse_bool(os.getenv('FINAL_APPLICATION_TESTING', 'false'), False)
if FINAL_APPLICATION_TESTING:
    QUICK_TESTING = False

DEBUG = parse_bool(os.getenv('DEBUG', 'false'), False)

if QUICK_TESTING:
    LLM_PROCESSING_ENABLED = False
    # STATEMENT_DECOMPOSITION_ENABLED = False
    print(f"QUICK_TESTING mode enabled - LLM_PROCESSING_ENABLED={LLM_PROCESSING_ENABLED}")
else:
    LLM_PROCESSING_ENABLED = parse_bool(os.getenv('LLM_PROCESSING_ENABLED', 'true'), True)
    # STATEMENT_DECOMPOSITION_ENABLED = os.getenv('STATEMENT_DECOMPOSITION_ENABLED', 'false').lower() == 'true'
    print(f"QUICK_TESTING mode disabled LLM_PROCESSING_ENABLED={LLM_PROCESSING_ENABLED}")

TIME_SENSITIVE_TRANSFORMATIONS_ENABLED = parse_bool(
    os.getenv('TIME_SENSITIVE_TRANSFORMATIONS_ENABLED', 'false'),
    False,
)

PROBABILITY_THRESHOLD = 0.4

ENRICHMENT_LIMIT = 10


def _default_remember_max_workers() -> int:
    cpu_count = resolve_cpu_count()
    computed = (cpu_count - 2) * 2
    return max(1, computed)


QUESTION_ANSWERING_MAX_BELIEFS = parse_int(
    os.getenv("QUESTION_ANSWERING_MAX_BELIEFS", "12"),
    12,
)

QUESTION_ANSWERING_MAX_ENTITY_DEPTH = parse_int(
    os.getenv("QUESTION_ANSWERING_MAX_ENTITY_DEPTH", "3"),
    3,
)

_max_sentences_per_input = parse_int(os.getenv("MAX_SENTENCES_PER_INPUT", "3"), 3)
MAX_SENTENCES_PER_INPUT = max(1, _max_sentences_per_input)

_remember_max_workers = parse_int(
    os.getenv("REMEMBER_MAX_WORKERS"),
    _default_remember_max_workers(),
)
REMEMBER_MAX_WORKERS = max(1, _remember_max_workers)

MULTITHREAD_REMEMBER_ENABLED = parse_bool(
    os.getenv("MULTITHREAD_REMEMBER_ENABLED"),
    True,
)

DEFAULT_SYNTHESIZED_TEXT_TYPE_VALUE = os.getenv("DEFAULT_SYNTHESIZED_TEXT_TYPE", "detailed")






# STATEMENT_DECOMPOSITION_ENABLED = os.getenv('STATEMENT_DECOMPOSITION_ENABLED', 'true').lower() == 'true'

print(f"Environment is set to {ENVIRONMENT} at Thinker")
