"""
Config file cho Supabase và các environment variables
Lấy giá trị từ file .env qua os.getenv
"""

import os

from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Export all config variables for type checking
__all__ = [
    "AI_MODEL_NAME",
    "AI_SUPABASE_KEY",
    "AI_SUPABASE_URL",
    "CHECKPOINT_POSTGRES_SCHEMA",
    "CHECKPOINT_POSTGRES_URL",
    "CLERK_SECRET_KEY",
    "CLERK_JWKS_URL",
    "CLERK_ISSUER",
    "CONV_DATABASE_URL",
    "CONV_SUPABASE_KEY",
    "CONV_SUPABASE_URL",
    "DEFAULT_MODEL",
    "FIRECRAWL_API_KEY",
    "GOOGLE_API_KEY",
    "GROQ_API_KEY",
    "JWT_ALGORITHM",
    "JWT_SECRET",
    "LANGFUSE_BASE_URL",
    "LANGFUSE_PUBLIC_KEY",
    "LANGFUSE_SECRET_KEY",
    "LANGSMITH_API_KEY",
    "LANGSMITH_ENDPOINT",
    "LANGSMITH_PROJECT",
    "LANGSMITH_TRACING",
    "MONGODB_DB_NAME",
    "MONGODB_URI",
    "OPENAI_API_KEY",
    "OTEL_EXPORTER_JAEGER_AGENT_HOST",
    "OTEL_EXPORTER_JAEGER_AGENT_PORT",
    "OTEL_EXPORTER_JAEGER_AGENT_SPLIT_OVERSIZED_BATCHES",
    "OTEL_SERVICE_NAME",
    "OTEL_TRACES_EXPORTER",
    "PORT",
    "REDIS_HOST",
    "REDIS_PASSWORD",
    "REDIS_PORT",
    "REDIS_USERNAME",
    "STARROCKS_DB",
    "STARROCKS_HOST",
    "STARROCKS_PASSWORD",
    "STARROCKS_PORT",
    "STARROCKS_USER",
    "USE_MONGO_CONVERSATION",
    "RATE_LIMIT_GUEST",
    "RATE_LIMIT_USER",
    "USE_SQLITE_HISTORY",
    "SQLITE_DB_PATH",
    "DISABLE_AUTH",
    "MEMO_DB_PATH",
]

# ====================== SUPABASE CONFIGURATION ======================
AI_SUPABASE_URL: str | None = os.getenv("AI_SUPABASE_URL")
AI_SUPABASE_KEY: str | None = os.getenv("AI_SUPABASE_KEY")
CONV_SUPABASE_URL: str | None = os.getenv("CONV_SUPABASE_URL")
CONV_SUPABASE_KEY: str | None = os.getenv("CONV_SUPABASE_KEY")

# ====================== REDIS CONFIGURATION ======================
REDIS_HOST: str | None = os.getenv("REDIS_HOST")
REDIS_PORT: int = int(os.getenv("REDIS_PORT", "6379"))
REDIS_PASSWORD: str | None = os.getenv("REDIS_PASSWORD")
REDIS_USERNAME: str | None = os.getenv("REDIS_USERNAME")

# ====================== AI API KEYS & MODELS ======================
OPENAI_API_KEY: str | None = os.getenv("OPENAI_API_KEY")
GOOGLE_API_KEY: str | None = os.getenv("GOOGLE_API_KEY")
GROQ_API_KEY: str | None = os.getenv("GROQ_API_KEY")

DEFAULT_MODEL: str = os.getenv("DEFAULT_MODEL", "gpt-4o-mini")
AI_MODEL_NAME = DEFAULT_MODEL

# ====================== JWT CONFIGURATION ======================
JWT_SECRET: str | None = os.getenv("JWT_SECRET")
JWT_ALGORITHM: str | None = os.getenv("JWT_ALGORITHM")

# ====================== SERVER CONFIG ======================
PORT: int = int(os.getenv("PORT", "5000"))
FIRECRAWL_API_KEY: str | None = os.getenv("FIRECRAWL_API_KEY")

# ====================== LANGFUSE CONFIGURATION (DEPRECATED) ======================
LANGFUSE_SECRET_KEY: str | None = os.getenv("LANGFUSE_SECRET_KEY")
LANGFUSE_PUBLIC_KEY: str | None = os.getenv("LANGFUSE_PUBLIC_KEY")
LANGFUSE_BASE_URL: str | None = os.getenv("LANGFUSE_BASE_URL", "https://cloud.langfuse.com")

# ====================== LANGSMITH CONFIGURATION (TẮT VÌ RATE LIMIT) ======================
# LANGSMITH_TRACING = os.getenv("LANGSMITH_TRACING", "false")
# LANGSMITH_ENDPOINT = os.getenv("LANGSMITH_ENDPOINT", "https://api.smith.langchain.com")
# LANGSMITH_API_KEY = os.getenv("LANGSMITH_API_KEY")
# LANGSMITH_PROJECT = os.getenv("LANGSMITH_PROJECT")
LANGSMITH_TRACING = "false"
LANGSMITH_ENDPOINT = None
LANGSMITH_API_KEY = None
LANGSMITH_PROJECT = None

# ====================== CLERK AUTHENTICATION ======================
CLERK_SECRET_KEY: str | None = os.getenv("CLERK_SECRET_KEY")
CLERK_JWKS_URL: str | None = os.getenv("CLERK_JWKS_URL")
CLERK_ISSUER: str | None = os.getenv("CLERK_ISSUER")

# ====================== DATABASE CONNECTION ======================
# Redis Cache Configuration
REDIS_CACHE_URL: str = os.getenv("REDIS_CACHE_URL", "172.16.2.192")
REDIS_CACHE_PORT: int = int(os.getenv("REDIS_CACHE_PORT", "6379"))
REDIS_CACHE_DB: int = int(os.getenv("REDIS_CACHE_DB", "2"))
REDIS_CACHE_TURN_ON: bool = os.getenv("REDIS_CACHE_TURN_ON", "true").lower() == "true"

CONV_DATABASE_URL: str | None = os.getenv("CONV_DATABASE_URL")

# ====================== MONGO CONFIGURATION ======================
MONGODB_URI: str | None = os.getenv("MONGODB_URI", "mongodb://localhost:27017")
MONGODB_DB_NAME: str | None = os.getenv("MONGODB_DB_NAME", "ai_law")
USE_MONGO_CONVERSATION: bool = os.getenv("USE_MONGO_CONVERSATION", "true").lower() == "true"

# ====================== CANIFA INTERNAL POSTGRES ======================
CHECKPOINT_POSTGRES_URL: str | None = os.getenv("CHECKPOINT_POSTGRES_URL")
CHECKPOINT_POSTGRES_SCHEMA: str = os.getenv("CHECKPOINT_POSTGRES_SCHEMA", "canifa_chat")

# ====================== STARROCKS DATA LAKE ======================
STARROCKS_HOST: str | None = os.getenv("STARROCKS_HOST")
STARROCKS_PORT: int = int(os.getenv("STARROCKS_PORT", "9030"))
STARROCKS_USER: str | None = os.getenv("STARROCKS_USER")
STARROCKS_PASSWORD: str | None = os.getenv("STARROCKS_PASSWORD")
STARROCKS_DB: str | None = os.getenv("STARROCKS_DB")

# Placeholder for backward compatibility if needed
# ====================== OPENTELEMETRY CONFIGURATION ======================
OTEL_EXPORTER_JAEGER_AGENT_HOST = os.getenv("OTEL_EXPORTER_JAEGER_AGENT_HOST")
OTEL_EXPORTER_JAEGER_AGENT_PORT = os.getenv("OTEL_EXPORTER_JAEGER_AGENT_PORT")
OTEL_SERVICE_NAME = os.getenv("OTEL_SERVICE_NAME")
OTEL_TRACES_EXPORTER = os.getenv("OTEL_TRACES_EXPORTER")
OTEL_EXPORTER_JAEGER_AGENT_SPLIT_OVERSIZED_BATCHES = os.getenv("OTEL_EXPORTER_JAEGER_AGENT_SPLIT_OVERSIZED_BATCHES")

RATE_LIMIT_GUEST: int = int(os.getenv("RATE_LIMIT_GUEST", "10"))
RATE_LIMIT_USER: int = int(os.getenv("RATE_LIMIT_USER", "100"))

# ====================== LOCAL DEV / TESTING TOGGLES ======================
# Turn off auth + rate limit middleware when true (for local testing)
DISABLE_AUTH: bool = os.getenv("DISABLE_AUTH", "false").lower() == "true"
# Use SQLite for chat history instead of Postgres when true
USE_SQLITE_HISTORY: bool = os.getenv("USE_SQLITE_HISTORY", "true").lower() == "true"
SQLITE_DB_PATH: str = os.getenv(
    "SQLITE_DB_PATH",
    os.path.join(os.path.dirname(__file__), "data", "chat_history.db"),
)

# Memo DB for note backend
MEMO_DB_PATH: str = os.getenv(
    "MEMO_DB_PATH",
    os.path.join(os.path.dirname(__file__), "db", "memos.db"),
)


