"""Module for global Constants."""

import os

# NOTE: Expected latency between SuperNode and SuperLink, 5 minutes
EXPECTED_LATENCY = 300

# Strategies states constants
MODEL_PARAMETERS = "current_server_parameters"
FIRST_MOMENTUM = "current_momentum_vector"
SECOND_MOMENTUM = "current_second_momentum_vector"

# Constant for the frequency dictionary name
FREQ_DICT_NAME = "1_gram.json"
FREQ_DICT_CACHE_NAME = "_freq_dict.json"

# Time-To-Live constants

# mt_dao_TTL: Time-To-Live for mt_dao operations. Default is 30 minutes.
# Previously reduced from 10 hours to 30 minutes to optimize system behavior.
mt_dao_TTL = int(
    os.getenv("mt_dao_TTL", None) or 30 * 60,
)  # Configurable via environment variable
FAILURE_MSG = "Failure"
SUCCESS_MSG = "Success"

BROADCAST_TTL = 2 * 60  # 2 minutes
MAX_BROADCAST_RETRIES = 5

BROADCAST = "broadcast"
BROADCAST_INS = "broadcast_ins"
BROADCAST_RES = "broadcast_res"
BROADCAST_P = "broadcast_parameters"
BROADCAST_MSG = "Broadcasting parameters"
BROADCAST_MSG_TYPE = "query.broadcast"

GATHER = "gather"
GATHER_INS = "gather_ins"
GATHER_RES = "gather_res"
GATHER_P = "gather_parameters"
GATHER_MSG = "Gathering parameters"
GATHER_MSG_TYPE = "query.gather"

FREE_RESOURCES = "free_resources"
FREE_RESOURCES_MSG_TYPE = "query.free_resources"

FIT = "fit"
FIT_INS = "fit_ins"
FIT_RES = "fit_res"

EVAL = "eval"
EVAL_INS = "eval_ins"
EVAL_RES = "eval_res"

QUERY = "query"

COMM_STACK = "comm_stack"
OBJECT_REFS = "object_refs"
MASK = "mask"
MASKS = "masks"
TRANSMISSION_MASK = "transmission_mask"
AGGREGATION_MASK = "aggregation_mask"
TRAINING_RESULTS = "training_results"
mt_dao_SERVER_PARAMETERS = "mt_dao_server_parameters"
mt_dao_CLIENT_PARAMETERS = "mt_dao_client_parameters"
BATCH_ID = "batch_id"
mt_dao_ARRAY_RECORD = "mt_dao_array_records"
METRICS = "metrics"
PARAMETERS = "parameters"
NUM_EXAMPLES = "num_examples"
NUM_SAMPLES = "num_samples"
LOSS = "loss"
EMPTY = "empty"

CLIENT_STATE_ACCUMULATOR = "client_state_accumulator"

CURRENT_SERVER_STATE_FILENAME = "current_server_state.bin"

CURRENT_CODEBASE_NAME = "mt_dao"
OLD_CODEBASE_NAME = "flower_llm"

DEFAULT_UNAVAILABLE_ENV_VAR = "None"

SERVER_ROUND = "server_round"
HISTORY = "history"
TIME_OFFSET = "time_offset"
CLIENT_STATES = "client_states"
CLIENT_STATE = "client_state"
CLIENT_ID = "client_id"
SERVER_STEPS_CUMULATIVE = "server_steps_cumulative"
KEY_LIGHTWEIGHT_SERVER_STATE = [
    SERVER_ROUND,
    HISTORY,
    TIME_OFFSET,
    CLIENT_STATES,
    SERVER_STEPS_CUMULATIVE,
]
LOCAL_STEPS_CUMULATIVE = "local_steps_cumulative"

EVALUATE_RESULTS = "evaluateres"
ENDPOINT_ID = "endpoint_id"
FOLDER_NAME = "folder_name"
FILE_NAME = "file_name"
QUERY = "query"
TYPE = "type"
CODE = "code"
MESSAGE = "message"
STATUS = "status"
OK = "OK"
SERVER_ROUND = "server_round"

DEFAULT_MAX_SHARD_SIZE = "5GB"

SAFETENSORS_SUFFIX = ".safetensors"
BIN_SUFFIX = ".bin"
NUMPY_ARRAY_SUFFIX = ".npz"
NUMPY_ARRAY_COMPRESSED_SUFFIX = ".npzc"
PYTORCH_SUFFIX = ".pt"

ALL = "all"

HIGH_WEIGHT_DECAY_WARNING = 1e-3

CENTRALIZED_LOG_SUFFIX = "_centralised"

mt_dao_SAVE_PATH = "mt_dao_SAVE_PATH"
CLIENTAPP_NO_RELOAD = "CLIENTAPP_NO_RELOAD"
MASTER_PORT = "MASTER_PORT"
MASTER_ADDR = "MASTER_ADDR"
LOCALHOST = "127.0.0.1"
PYTHONUNBUFFERED = "PYTHONUNBUFFERED"
TORCH_NCCL_ASYNC_ERROR_HANDLING = "TORCH_NCCL_ASYNC_ERROR_HANDLING"
RANK = "RANK"
NODE_RANK = "NODE_RANK"
LOCAL_RANK = "LOCAL_RANK"
RUN_UUID = "RUN_UUID"
APPOINTED_CUDA_DEVICE = "APPOINTED_CUDA_DEVICE"
WORLD_SIZE = "WORLD_SIZE"
LOCAL_WORLD_SIZE = "LOCAL_WORLD_SIZE"

SERVER_PROCESS_NAME = "server"
CUSTOM_RAY_GC = "CustomRayGarbageCollector"
CUSTOM_SHM_GC = "CustomShmGarbageCollector"

CONFIG_FILE_YAML = "config.yaml"

NUMBER_MODEL_STATES = 3
