# app/src/core/config.py

from __future__ import annotations

import os
from dataclasses import dataclass
from pathlib import Path

from dotenv import load_dotenv


# корень проекта
BASE_DIR = Path(__file__).resolve().parents[2]

# .env файл
ENV_FILE = BASE_DIR / ".env"

# загружаем переменные окружения
load_dotenv(ENV_FILE)


@dataclass(slots=True)
class Settings:
    # Telegram
    bot_token: str
    bot_parse_mode: str

    # App
    app_env: str
    log_level: str
    tz: str

    # Exchange
    exchange_enabled: bool
    exchange_name: str
    exchange_base_url: str
    exchange_ws_url: str
    exchange_api_key: str
    exchange_api_secret: str
    exchange_timeout_sec: int
    exchange_testnet: bool
    default_symbol: str

    # Database
    db_host: str
    db_port: int
    db_name: str
    db_user: str
    db_password: str

    # Debug helpers
    debug_enabled: bool
    journal_debug_enabled: bool

    # helper: demo/live mode
    def is_demo_mode(self) -> bool:
        return "demo" in self.exchange_base_url.lower()


# parse bool
def _parse_bool(raw_value: str, default: bool = False) -> bool:
    value = (raw_value or "").strip().lower()
    if not value:
        return default

    return value in {"1", "true", "yes", "on"}


# parse int
def _parse_int(raw_value: str, default: int) -> int:
    value = (raw_value or "").strip()
    if not value:
        return default

    return int(value)


# load all settings
def load_settings() -> Settings:
    bot_token = os.getenv("BOT_TOKEN", "").strip()

    if not bot_token:
        raise RuntimeError("BOT_TOKEN is not set in app/.env")

    return Settings(
        # Telegram
        bot_token=bot_token,
        bot_parse_mode=os.getenv("BOT_PARSE_MODE", "HTML").strip() or "HTML",

        # App
        app_env=os.getenv("APP_ENV", "dev").strip() or "dev",
        log_level=os.getenv("LOG_LEVEL", "INFO").strip().upper() or "INFO",
        tz=os.getenv("TZ", "Europe/Minsk").strip() or "Europe/Minsk",
        debug_enabled=_parse_bool(os.getenv("DEBUG_ENABLED", "false")),
        journal_debug_enabled=_parse_bool(
            os.getenv("JOURNAL_DEBUG_ENABLED", "false")
        ),

        # Exchange
        exchange_enabled=_parse_bool(os.getenv("EXCHANGE_ENABLED", "false")),
        exchange_name=os.getenv("EXCHANGE_NAME", "dzengi").strip() or "dzengi",
        exchange_base_url=os.getenv("EXCHANGE_BASE_URL", "").strip(),
        exchange_ws_url=os.getenv("EXCHANGE_WS_URL", "").strip(),
        exchange_api_key=os.getenv("EXCHANGE_API_KEY", "").strip(),
        exchange_api_secret=os.getenv("EXCHANGE_API_SECRET", "").strip(),
        exchange_timeout_sec=_parse_int(os.getenv("EXCHANGE_TIMEOUT_SEC", "10"), 10),
        exchange_testnet=_parse_bool(os.getenv("EXCHANGE_TESTNET", "false")),
        default_symbol=os.getenv("DEFAULT_SYMBOL", "ETH/USD_LEVERAGE").strip()
        or "ETH/USD_LEVERAGE",

        # Database
        db_host=os.getenv("DB_HOST", "localhost").strip() or "localhost",
        db_port=_parse_int(os.getenv("DB_PORT", "5432"), 5432),
        db_name=os.getenv("DB_NAME", "dzentra_bot").strip() or "dzentra_bot",
        db_user=os.getenv("DB_USER", "dzentra_bot").strip() or "dzentra_bot",
        db_password=os.getenv("DB_PASSWORD", "").strip(),
    )