import sys
import os
import base64
from pathlib import Path
from navconfig import config, BASE_DIR
from navconfig.logging import logging
from navigator.conf import default_dsn, CACHE_HOST, CACHE_PORT


# # disable debug on some libraries:
# logging.getLogger(name='httpcore').setLevel(logging.INFO)
# logging.getLogger(name='httpx').setLevel(logging.INFO)
# logging.getLogger(name='groq').setLevel(logging.INFO)
# logging.getLogger(name='selenium.webdriver').setLevel(logging.WARNING)
# logging.getLogger(name='selenium').setLevel(logging.INFO)
# logging.getLogger(name='matplotlib').setLevel(logging.WARNING)
# logging.getLogger(name='PIL').setLevel(logging.INFO)
logging.getLogger("grpc").setLevel(logging.ERROR)
os.environ['GRPC_VERBOSITY'] = 'ERROR'
# logging.getLogger("weasyprint").setLevel(logging.ERROR)  # Suppress WeasyPrint warnings
# # Suppress tiktoken warnings
# logging.getLogger("tiktoken").setLevel(logging.ERROR)
# logging.getLogger("fontTools").setLevel(logging.ERROR)

# Project Root:
PROJECT_ROOT = BASE_DIR
# Plugins Directory:
PLUGINS_DIR = config.get('PLUGINS_DIR', fallback=BASE_DIR.joinpath('plugins'))
if isinstance(PLUGINS_DIR, str):
    PLUGINS_DIR = Path(PLUGINS_DIR).resolve()
if not PLUGINS_DIR.exists():
    PLUGINS_DIR.mkdir(parents=True, exist_ok=True)

# Static directory
STATIC_DIR = config.get('STATIC_DIR', fallback=BASE_DIR.joinpath('static'))
if isinstance(STATIC_DIR, str):
    STATIC_DIR = Path(STATIC_DIR)

# Output directory (default base for tool-generated files)
OUTPUT_DIR = Path(
    config.get('OUTPUT_DIR', fallback=BASE_DIR.joinpath('outputs'))
)
if not OUTPUT_DIR.is_absolute():
    OUTPUT_DIR = BASE_DIR.joinpath(OUTPUT_DIR)
if not OUTPUT_DIR.exists():
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)


# Main Database:
# DB Default (database used for interaction (rw))
DBHOST = config.get('DBHOST', fallback='localhost')
DBUSER = config.get('DBUSER')
DBPWD = config.get('DBPWD')
DBNAME = config.get('DBNAME', fallback='navigator')
DBPORT = config.get('DBPORT', fallback=5432)
default_dsn = f'postgres://{DBUSER}:{DBPWD}@{DBHOST}:{DBPORT}/{DBNAME}'
async_default_dsn = f'postgresql+asyncpg://{DBUSER}:{DBPWD}@{DBHOST}:{DBPORT}/{DBNAME}'
sqlalchemy_url = f'postgresql://{DBUSER}:{DBPWD}@{DBHOST}:{DBPORT}/{DBNAME}'


# Environment
ENVIRONMENT = config.get("ENVIRONMENT", fallback="development")
ENABLE_SWAGGER = config.getboolean("ENABLE_SWAGGER", fallback=False)
ENABLE_DASHBOARDS = config.getboolean("ENABLE_DASHBOARDS", fallback=False)
ENABLE_CREWS = config.getboolean("ENABLE_CREWS", fallback=False)
ENABLE_DATABASE_BOTS = config.getboolean("ENABLE_DATABASE_BOTS", fallback=False)
ENABLE_REGISTRY_BOTS = config.getboolean("ENABLE_REGISTRY_BOTS", fallback=True)


# Planogram images directory
PLANOGRAM_FOLDER = Path(
    config.get('PLANOGRAM_FOLDER', fallback=BASE_DIR.joinpath('images'))
)
if not PLANOGRAM_FOLDER.is_absolute():
    PLANOGRAM_FOLDER = BASE_DIR.joinpath(PLANOGRAM_FOLDER)

# ── Ontology Configuration ──
# Base directory for ontology YAML files (base + domains + clients).
ONTOLOGY_DIR = Path(
    config.get('ONTOLOGY_DIR', fallback=BASE_DIR.joinpath('ontologies'))
)
if isinstance(ONTOLOGY_DIR, str):
    ONTOLOGY_DIR = Path(ONTOLOGY_DIR).resolve()
if not ONTOLOGY_DIR.exists():
    ONTOLOGY_DIR.mkdir(parents=True, exist_ok=True)

# Base ontology filename — foundational layer all tenants inherit.
ONTOLOGY_BASE_FILE = config.get('ONTOLOGY_BASE_FILE', fallback='base.ontology.yaml')

# Subdirectory for domain-specific ontology extensions.
ONTOLOGY_DOMAINS_DIR = config.get('ONTOLOGY_DOMAINS_DIR', fallback='domains')

# Subdirectory for client-specific ontology overrides.
ONTOLOGY_CLIENTS_DIR = config.get('ONTOLOGY_CLIENTS_DIR', fallback='clients')

# Global on/off switch for ontology-based RAG.
ENABLE_ONTOLOGY_RAG = config.getboolean('ENABLE_ONTOLOGY_RAG', fallback=False)

# ArangoDB database naming template per tenant ({tenant} is replaced at runtime).
ONTOLOGY_DB_TEMPLATE = config.get('ONTOLOGY_DB_TEMPLATE', fallback='{tenant}_ontology')

# PgVector schema naming template per tenant.
ONTOLOGY_PGVECTOR_SCHEMA_TEMPLATE = config.get(
    'ONTOLOGY_PGVECTOR_SCHEMA_TEMPLATE', fallback='{tenant}'
)

# Redis key prefix for ontology traversal cache.
ONTOLOGY_CACHE_PREFIX = config.get('ONTOLOGY_CACHE_PREFIX', fallback='parrot:ontology')

# TTL for cached pipeline results in seconds (86400 = 24h, aligned with CRON refresh).
ONTOLOGY_CACHE_TTL = config.getint('ONTOLOGY_CACHE_TTL', fallback=86400)

# Maximum depth for dynamic AQL traversals generated by the LLM (security guardrail).
ONTOLOGY_MAX_TRAVERSAL_DEPTH = config.getint('ONTOLOGY_MAX_TRAVERSAL_DEPTH', fallback=4)

# LLM model for dynamic AQL generation and intent detection.
ONTOLOGY_AQL_MODEL = config.get('ONTOLOGY_AQL_MODEL', fallback='gemini-2.5-flash')

# Directory for review queue JSON files (ambiguous relation matches).
# Defaults to {ONTOLOGY_DIR}/review/ at runtime when None.
ONTOLOGY_REVIEW_DIR = config.get('ONTOLOGY_REVIEW_DIR', fallback=None)
if ONTOLOGY_REVIEW_DIR is not None:
    ONTOLOGY_REVIEW_DIR = Path(ONTOLOGY_REVIEW_DIR).resolve()

# Agents Directory
AGENTS_DIR = config.get('AGENTS_DIR', fallback=BASE_DIR.joinpath('agents'))
if isinstance(AGENTS_DIR, str):
    AGENTS_DIR = Path(AGENTS_DIR).resolve()
if not AGENTS_DIR.exists():
    AGENTS_DIR.mkdir(parents=True, exist_ok=True)

# Add AGENTS_DIR to sys.path for direct imports (e.g., from agents.troc import ...)
# Remove if already present to avoid duplicates, then insert at position 0
# This ensures AGENTS_DIR takes precedence over PLUGINS_DIR even if plugins/__init__.py
# has already inserted PLUGINS_DIR at position 0
agents_dir_str = str(AGENTS_DIR)
if agents_dir_str in sys.path:
    sys.path.remove(agents_dir_str)
sys.path.insert(0, agents_dir_str)


# MCP Server Directory:
MCP_SERVER_DIR = config.get(
    'MCP_SERVER_DIR',
    fallback=BASE_DIR.joinpath('mcp_servers')
)
if isinstance(MCP_SERVER_DIR, str):
    MCP_SERVER_DIR = Path(MCP_SERVER_DIR).resolve()
if not MCP_SERVER_DIR.exists():
    MCP_SERVER_DIR.mkdir(parents=True, exist_ok=True)

# Docker file location (for generated docker-compose files, Dockerfiles, etc.)
DOCKER_FILE_LOCATION = config.get(
    'DOCKER_FILE_LOCATION',
    fallback=BASE_DIR.joinpath('docker')
)
if isinstance(DOCKER_FILE_LOCATION, str):
    DOCKER_FILE_LOCATION = Path(DOCKER_FILE_LOCATION).resolve()

# MCP Server defaults
MCP_SERVER_TRANSPORT = config.get('MCP_SERVER_TRANSPORT', fallback='http')
MCP_SERVER_HOST = config.get('MCP_SERVER_HOST', fallback='127.0.0.1')
MCP_SERVER_PORT = config.getint('MCP_SERVER_PORT', fallback=9090)
MCP_SERVER_NAME = config.get('MCP_SERVER_NAME', fallback='ai-parrot-tools')
MCP_SERVER_DESCRIPTION = config.get(
    'MCP_SERVER_DESCRIPTION',
    fallback='AI-Parrot MCP Tooling'
)
MCP_SERVER_LOG_LEVEL = config.get('MCP_SERVER_LOG_LEVEL', fallback='INFO')

# Default tools that should be started with the MCP server
MCP_STARTED_TOOLS = {
    # 'MSTeamsToolkit': 'parrot.tools.msteams',
    # 'PDFPrintTool': 'parrot.tools.pdfprint',
    'JiraToolkit': 'parrot.tools.jiratoolkit',
}

# Agents-Bots Prompt directory:
AGENTS_BOTS_PROMPT_DIR = config.get(
    'AGENTS_BOTS_PROMPT_DIR',
    fallback=AGENTS_DIR.joinpath('prompts')
)
if isinstance(AGENTS_BOTS_PROMPT_DIR, str):
    AGENTS_BOTS_PROMPT_DIR = Path(AGENTS_BOTS_PROMPT_DIR).resolve()
if not AGENTS_BOTS_PROMPT_DIR.exists():
    AGENTS_BOTS_PROMPT_DIR.mkdir(parents=True, exist_ok=True)

# LLM Model
DEFAULT_LLM_MODEL_NAME = config.get('LLM_MODEL_NAME', fallback='gemini-2.5-pro')


## MILVUS DB ##:
MILVUS_HOST = config.get('MILVUS_HOST', fallback='localhost')
MILVUS_PROTOCOL = config.get('MILVUS_PROTOCOL', fallback='http')
MILVUS_PORT = config.get('MILVUS_PORT', fallback=19530)
MILVUS_URL = config.get('MILVUS_URL')
MILVUS_TOKEN = config.get('MILVUS_TOKEN')
MILVUS_USER = config.get('MILVUS_USER')
MILVUS_PASSWORD = config.get('MILVUS_PASSWORD')
MILVUS_SECURE = config.getboolean('MILVUS_SECURE', fallback=False)
MILVUS_SERVER_NAME = config.get(
    'MILVUS_SERVER_NAME'
)
MILVUS_CA_CERT = config.get('MILVUS_CA_CERT', fallback=None)
MILVUS_SERVER_CERT = config.get('MILVUS_SERVER_CERT', fallback=None)
MILVUS_SERVER_KEY = config.get('MILVUS_SERVER_KEY', fallback=None)
MILVUS_USE_TLSv2 = config.getboolean('MILVUS_USE_TLSv2', fallback=False)

# Postgres Database:
DBHOST = config.get("DBHOST", fallback="localhost")
DBUSER = config.get("DBUSER")
DBPWD = config.get("DBPWD")
DBNAME = config.get("DBNAME", fallback="navigator")
DBPORT = config.get("DBPORT", fallback=5432)
# sqlalchemy+asyncpg connector:
default_sqlalchemy_pg = f"postgresql+asyncpg://{DBUSER}:{DBPWD}@{DBHOST}:{DBPORT}/{DBNAME}"

PG_USER = config.get('PG_USER', fallback=DBUSER)
PG_PWD = config.get('PG_PWD', fallback=DBPWD)
PG_HOST = config.get('PG_HOST', fallback=DBHOST)
PG_PORT = config.get('PG_PORT', fallback=DBPORT)
PG_DATABASE = config.get('PG_DATABASE', fallback=DBNAME)
# asyncpg url for sqlalchemy:
asyncpg_sqlalchemy_url = f"postgresql+asyncpg://{PG_USER}:{PG_PWD}@{PG_HOST}:{PG_PORT}/{PG_DATABASE}"

# ScyllaDB Database:
SCYLLADB_DRIVER = config.get('SCYLLADB_DRIVER', fallback='scylladb')
SCYLLADB_HOST = config.get('SCYLLADB_HOST', fallback='localhost')
SCYLLADB_PORT = config.getint('SCYLLADB_PORT', fallback=9042)
SCYLLADB_USERNAME = config.get('SCYLLADB_USERNAME', fallback='navigator')
SCYLLADB_PASSWORD = config.get('SCYLLADB_PASSWORD', fallback='navigator')
SCYLLADB_KEYSPACE = config.get('SCYLLADB_KEYSPACE', fallback='navigator')


# BigQuery Configuration:
BIGQUERY_CREDENTIALS = config.get('BIGQUERY_CREDENTIALS')
BIGQUERY_PROJECT_ID = config.get('BIGQUERY_PROJECT_ID', fallback='navigator')
BIGQUERY_DATASET = config.get('BIGQUERY_DATASET', fallback='navigator')

# Redis History Configuration:
REDIS_HOST = config.get('REDIS_HOST', fallback='localhost')
REDIS_PORT = config.get('REDIS_PORT', fallback=6379)
REDIS_DB = config.get('REDIS_DB', fallback=1)
REDIS_URL = config.get('REDIS_URL', fallback=f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}")
REDIS_HISTORY_DB = config.get('REDIS_HISTORY_DB', fallback=3)
REDIS_HISTORY_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_HISTORY_DB}"
REDIS_SERVICES_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/4"
REDIS_DATASET_DB = config.get('REDIS_DATASET_DB', fallback=3)
REDIS_DATASET_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DATASET_DB}"

def resolve_cert(crt):
    cert = Path(crt)
    if not cert.is_absolute():
        cert = BASE_DIR.joinpath(cert)
    else:
        cert.resolve()
    return cert

if MILVUS_SERVER_CERT:
    MILVUS_SERVER_CERT = str(resolve_cert(MILVUS_SERVER_CERT))
if MILVUS_CA_CERT:
    MILVUS_CA_CERT = str(resolve_cert(MILVUS_CA_CERT))
if MILVUS_SERVER_KEY:
    MILVUS_SERVER_KEY = str(resolve_cert(MILVUS_SERVER_KEY))

# QDRANT:
QDRANT_PROTOCOL = config.get('QDRANT_PROTOCOL', fallback='http')
QDRANT_HOST = config.get('QDRANT_HOST', fallback='localhost')
QDRANT_PORT = config.get('QDRANT_PORT', fallback=6333)
QDRANT_USE_HTTPS = config.getboolean('QDRANT_USE_HTTPS', fallback=False)
QDRANT_URL = config.get('QDRANT_URL')
# QDRANT Connection Type: server or cloud
QDRANT_CONN_TYPE = config.get('QDRANT_CONN_TYPE', fallback='server')

# ChromaDB:
CHROMADB_HOST = config.get('CHROMADB_HOST', fallback='localhost')
CHROMADB_PORT = config.get('CHROMADB_PORT', fallback=8000)

# Embedding Device:
EMBEDDING_DEVICE = config.get('EMBEDDING_DEVICE', fallback='cpu')
EMBEDDING_DEFAULT_MODEL = config.get(
    'EMBEDDING_DEFAULT_MODEL',
    fallback='sentence-transformers/all-MiniLM-L12-v2'
)
EMBEDDING_REGISTRY_MAX_MODELS = int(
    os.getenv('EMBEDDING_REGISTRY_MAX_MODELS', '10')
)
KB_DEFAULT_MODEL = config.get(
    'KB_DEFAULT_MODEL',
    fallback='sentence-transformers/paraphrase-MiniLM-L3-v2'
)
HUGGINGFACE_EMBEDDING_CACHE_DIR = config.get(
    'HUGGINGFACE_EMBEDDING_CACHE_DIR',
    fallback=BASE_DIR.joinpath('model_cache', 'huggingface')
)
HUGGINGFACEHUB_API_TOKEN = config.get('HUGGINGFACEHUB_API_TOKEN')
MAX_VRAM_AVAILABLE = config.get('MAX_VRAM_AVAILABLE', fallback=20000)
RAM_AVAILABLE = config.get('RAM_AVAILABLE', fallback=819200)
CUDA_DEFAULT_DEVICE = config.get('CUDA_DEFAULT_DEVICE', fallback='cpu')
CUDA_DEFAULT_DEVICE_NUMBER = config.getint('CUDA_DEFAULT_DEVICE_NUMBER', fallback=0)
MAX_BATCH_SIZE = config.get('MAX_BATCH_SIZE', fallback=2048)

# Enable Teams Bot:
ENABLE_AZURE_BOT = config.getboolean('ENABLE_AZURE_BOT', fallback=True)

## Google Services:
GOOGLE_API_KEY = config.get('GOOGLE_API_KEY')
### Google Service Credentials:
GA_SERVICE_ACCOUNT_NAME = config.get('GA_SERVICE_ACCOUNT_NAME', fallback="google.json")
GA_SERVICE_PATH = config.get('GA_SERVICE_PATH', fallback="env/google/")
if isinstance(GA_SERVICE_PATH, str):
    GA_SERVICE_PATH = Path(GA_SERVICE_PATH)

GOOGLE_TTS_SERVICE = config.get(
    'GOOGLE_TTS_SERVICE',
    fallback=GA_SERVICE_PATH.joinpath('tts-service.json')
)
if isinstance(GOOGLE_TTS_SERVICE, str):
    GOOGLE_TTS_SERVICE = Path(GOOGLE_TTS_SERVICE)
if not GOOGLE_TTS_SERVICE.is_absolute():
    GOOGLE_TTS_SERVICE = BASE_DIR.joinpath(GOOGLE_TTS_SERVICE)
if not GOOGLE_TTS_SERVICE.exists():
    GOOGLE_TTS_SERVICE = None

# BASE STATIC:
BASE_STATIC_URL = config.get(
    'BASE_STATIC_URL',
    fallback='http://localhost:5000/static'
)

# Google SerpAPI:
SERPAPI_API_KEY = config.get('SERPAPI_API_KEY')

# Groq API Key:
GROQ_API_KEY = config.get('GROQ_API_KEY')
DEFAULT_GROQ_MODEL = config.get('DEFAULT_GROQ_MODEL', fallback='qwen/qwen3-32b')

# Ethical Principle:
ETHICAL_PRINCIPLE = config.get(
    'ETHICAL_PRINCIPLE',
    fallback='The model should only talk about ethical and legal things.'
)

# Embedding Configuration:

# VERTEX
VERTEX_PROJECT_ID = config.get('VERTEX_PROJECT_ID')
VERTEX_REGION = config.get('VERTEX_REGION')

# OpenAI:
OPENAI_API_KEY = config.get('OPENAI_API_KEY')
OPENAI_ORGANIZATION = config.get('OPENAI_ORGANIZATION')

## HTTPClioent
HTTPCLIENT_MAX_SEMAPHORE = config.getint("HTTPCLIENT_MAX_SEMAPHORE", fallback=5)
HTTPCLIENT_MAX_WORKERS = config.getint("HTTPCLIENT_MAX_WORKERS", fallback=1)

## Google API:
GOOGLE_API_KEY = config.get('GOOGLE_API_KEY')
GOOGLE_SEARCH_API_KEY = config.get('GOOGLE_SEARCH_API_KEY')
GOOGLE_SEARCH_ENGINE_ID = config.get('GOOGLE_SEARCH_ENGINE_ID')
GOOGLE_PLACES_API_KEY = config.get('GOOGLE_PLACES_API_KEY')
GOOGLE_CREDENTIALS_FILE = Path(
    config.get(
        'GOOGLE_CREDENTIALS_FILE',
        fallback=BASE_DIR.joinpath('env', 'google', 'key.json')
    )
)

## LLM default config:
DEFAULT_LLM_MODEL = config.get('LLM_MODEL', fallback='gemini-2.5-flash')
DEFAULT_LLM_TEMPERATURE = config.get('LLM_TEMPERATURE', fallback=0.1)

"""
Amazon AWS Credentials
"""
aws_region = config.get("AWS_REGION", fallback="us-east-1")
aws_bucket = config.get("AWS_BUCKET", fallback="static-files")
aws_key = config.get("AWS_KEY")
aws_secret = config.get("AWS_SECRET")

AWS_ACCESS_KEY = config.get("AWS_ACCESS_KEY", fallback=aws_key)
AWS_SECRET_KEY = config.get("AWS_SECRET_KEY", fallback=aws_secret)
AWS_REGION_NAME = config.get("AWS_REGION_NAME", fallback=aws_region)
AWS_DEFAULT_CLOUDWATCH_LOG_GROUP = config.get(
    "AWS_DEFAULT_CLOUDWATCH_LOG_GROUP",
    fallback="/parrot/logs"
)

AWS_CREDENTIALS = {
    "default": {
        "use_credentials": config.get("aws_credentials", fallback=False),
        "aws_key": aws_key,
        "aws_secret": aws_secret,
        "region_name": aws_region,
        "bucket_name": aws_bucket,
    },
    "monitoring": {
        "use_credentials": config.get("aws_monitor_credentials", fallback=True),
        "aws_key": AWS_ACCESS_KEY,
        "aws_secret": AWS_SECRET_KEY,
        "region_name": AWS_REGION_NAME,
    },
    "cloudwatch": {
        "use_credentials": True,
        "aws_key": config.get("AWS_CLOUDWATCH_KEY"),
        "aws_secret": config.get("AWS_CLOUDWATCH_SECRET"),
        "region_name": config.get("AWS_CLOUDWATCH_REGION", fallback="us-east-1"),
    }
}

## Tools:
OPENWEATHER_APPID = config.get('OPENWEATHER_APPID')

# NOTIFICATIONS:
TEAMS_NOTIFY_TENANT_ID = config.get("TEAMS_NOTIFY_TENANT_ID")
TEAMS_NOTIFY_CLIENT_ID = config.get("TEAMS_NOTIFY_CLIENT_ID")
TEAMS_NOTIFY_CLIENT_SECRET = config.get("TEAMS_NOTIFY_CLIENT_SECRET")
TEAMS_NOTIFY_USERNAME = config.get("TEAMS_NOTIFY_USERNAME")
TEAMS_NOTIFY_PASSWORD = config.get("TEAMS_NOTIFY_PASSWORD")
MS_TEAMS_DEFAULT_TEAMS_ID = config.get("MS_TEAMS_DEFAULT_TEAMS_ID")
MS_TEAMS_DEFAULT_CHANNEL_ID = config.get("MS_TEAMS_DEFAULT_CHANNEL_ID")

## MS Teams Toolkit:
MS_TEAMS_CLIENT_SECRET = config.get('MS_TEAMS_CLIENT_SECRET')
MS_TEAMS_CLIENT_ID = config.get('MS_TEAMS_CLIENT_ID')
MS_TEAMS_TENANT_ID = config.get('MS_TEAMS_TENANT_ID')
MS_TEAMS_USERNAME = config.get('TEAMS_NOTIFY_USERNAME')
MS_TEAMS_PASSWORD = config.get('TEAMS_NOTIFY_PASSWORD')

## Office 365:
O365_CLIENT_ID = config.get('O365_CLIENT_ID')
O365_CLIENT_SECRET = config.get('O365_CLIENT_SECRET')
O365_TENANT_ID = config.get('O365_TENANT_ID')

# Sharepoint:
SHAREPOINT_APP_ID = config.get('SHAREPOINT_APP_ID')
SHAREPOINT_APP_SECRET = config.get('SHAREPOINT_APP_SECRET')
SHAREPOINT_TENANT_ID = config.get('SHAREPOINT_TENANT_ID')
SHAREPOINT_TENANT_NAME = config.get('SHAREPOINT_TENANT_NAME')
SHAREPOINT_SITE_ID = config.get('SHAREPOINT_SITE_ID')
SHAREPOINT_DEFAULT_HOST = config.get('SHAREPOINT_DEFAULT_HOST')

# Employee Hierarchy Configuration:
EMPLOYEES_TABLE = config.get('EMPLOYEES_TABLE', fallback='troc.troc_employees')

# Workday SOAP settings
WORKDAY_DEFAULT_TENANT = config.get('WORKDAY_DEFAULT_TENANT', fallback='nav')
WORKDAY_CLIENT_ID = config.get("WORKDAY_CLIENT_ID")
WORKDAY_CLIENT_SECRET = config.get("WORKDAY_CLIENT_SECRET")
WORKDAY_TOKEN_URL = config.get("WORKDAY_TOKEN_URL")
WORKDAY_WSDL_PATH = config.get(
    "WORKDAY_WSDL_PATH",
    fallback=BASE_DIR.joinpath("env", "workday", "staffing_custom_44_2.wsdl")
)
WORKDAY_WSDL_TIME = config.get(
    "WORKDAY_WSDL_TIME",
    fallback=BASE_DIR.joinpath("env", "workday", "timetracking_custom_44_2.wsdl")
)
WORKDAY_WSDL_HUMAN_RESOURCES = config.get(
    "WORKDAY_WSDL_HUMAN_RESOURCES",
    fallback=BASE_DIR.joinpath("env", "workday", "humanresources_troc_44_2.wsdl")
)
WORKDAY_WSDL_FINANCIAL_MANAGEMENT = config.get(
    "WORKDAY_WSDL_FINANCIAL_MANAGEMENT",
    fallback=BASE_DIR.joinpath("env", "workday", "financial_management_45.wsdl")
)
WORKDAY_WSDL_RECRUITING = config.get(
    "WORKDAY_WSDL_RECRUITING",
    fallback=BASE_DIR.joinpath("env", "workday", "recruiting_44_2.wsdl")
)
WORKDAY_WSDL_ABSENCE_MANAGEMENT = config.get(
    "WORKDAY_WSDL_ABSENCE_MANAGEMENT",
    fallback=BASE_DIR.joinpath("env", "workday", "absence_management_45_custom.wsdl")
)
WORKDAY_WSDL_PAYROLL = config.get(
    "WORKDAY_WSDL_PAYROLL",
    fallback=BASE_DIR.joinpath("env", "workday", "payroll_v45_2.wsdl")
)
WORKDAY_REFRESH_TOKEN = config.get("WORKDAY_REFRESH_TOKEN", fallback=None)
WORKDAY_REPORT_USERNAME = config.get("WORKDAY_REPORT_USERNAME", fallback=None)
WORKDAY_REPORT_PASSWORD = config.get("WORKDAY_REPORT_PASSWORD", fallback=None)
WORKDAY_REPORT_PASSWORD_BASE64 = config.get("WORKDAY_REPORT_PASSWORD_BASE64", fallback=None)
if WORKDAY_REPORT_PASSWORD_BASE64 and not WORKDAY_REPORT_PASSWORD:
    WORKDAY_REPORT_PASSWORD = base64.b64decode(WORKDAY_REPORT_PASSWORD_BASE64).decode("utf-8")
WORKDAY_REPORT_OWNER = config.get("WORKDAY_REPORT_OWNER", fallback=None)
WORKDAY_URL = config.get("WORKDAY_URL", fallback="https://services1.wd501.myworkday.com")

WORKDAY_WSDL_PATHS = {
    "human_resources": WORKDAY_WSDL_HUMAN_RESOURCES,
    "absence_management": WORKDAY_WSDL_ABSENCE_MANAGEMENT,
    "time_tracking": WORKDAY_WSDL_TIME,
    "staffing": WORKDAY_WSDL_PATH,
    "financial_management": WORKDAY_WSDL_FINANCIAL_MANAGEMENT,
    "recruiting": WORKDAY_WSDL_RECRUITING,
    "payroll": WORKDAY_WSDL_PAYROLL
}

# Final sys.path adjustment: Ensure AGENTS_DIR takes precedence over PLUGINS_DIR
# This is necessary because parrot.plugins.__init__.py may have inserted PLUGINS_DIR
# at position 0 during module loading (after our initial AGENTS_DIR insertion above)
if agents_dir_str in sys.path:
    sys.path.remove(agents_dir_str)
sys.path.insert(0, agents_dir_str)


# WhatsApp Bridge:
WHATSAPP_BRIDGE_ENABLED = config.get(
    'WHATSAPP_BRIDGE_ENABLED',
    fallback=True
)
WHATSAPP_BRIDGE_URL = config.get(
    'WHATSAPP_BRIDGE_URL',
    fallback='http://localhost:8765'
)
WHATSAPP_ALLOWED_PHONES = config.get(
    'WHATSAPP_ALLOWED_PHONES',
    fallback=None  # None = allow all
)
WHATSAPP_ALLOWED_GROUPS = config.get(
    'WHATSAPP_ALLOWED_GROUPS',
    fallback=None  # None = allow all
)
WHATSAPP_COMMAND_PREFIX = config.get(
    'WHATSAPP_COMMAND_PREFIX',
    fallback=''  # Empty = no prefix required
)

JIRA_USERS = [
    {
        "id": "35",
        "name": "Jesus Lara",
        "jira_username": "jesuslarag@gmail.com",
        "telegram_chat_id": "286137732",
        "manager_chat_id": "286137732",
        "username": "jlara@trocglobal.com"
    }
]

## Security:
AWS_ACCESS_KEY_ID = config.get("AWS_ACCESS_KEY_ID", fallback=AWS_ACCESS_KEY)
AWS_SECRET_ACCESS_KEY = config.get("AWS_SECRET_ACCESS_KEY", fallback=AWS_SECRET_KEY)
AWS_DEFAULT_REGION = config.get("AWS_DEFAULT_REGION", fallback=AWS_REGION_NAME)

# ── Odoo ERP (JSON-RPC 2.0) ──
ODOO_URL = config.get("ODOO_URL", fallback=None)
ODOO_DATABASE = config.get("ODOO_DATABASE", fallback=None)
ODOO_USERNAME = config.get("ODOO_USERNAME", fallback=None)
ODOO_PASSWORD = config.get("ODOO_PASSWORD", fallback=None)
ODOO_TIMEOUT = config.getint("ODOO_TIMEOUT", fallback=30)
ODOO_VERIFY_SSL = config.getboolean("ODOO_VERIFY_SSL", fallback=True)
