"""Signal V2 환경변수 로딩."""
import os
from dataclasses import dataclass, field
from pathlib import Path

from dotenv import load_dotenv

load_dotenv(Path(__file__).parent.parent / ".env")


@dataclass(frozen=True)
class Settings:
    stock_api_url: str = field(
        default_factory=lambda: os.getenv("STOCK_API_URL", "").rstrip("/")
    )
    webai_api_key: str = field(
        default_factory=lambda: os.getenv("WEBAI_API_KEY", "").strip()
    )
    port: int = field(default_factory=lambda: int(os.getenv("SIGNAL_V2_PORT", "8001")))
    db_path: Path = field(
        default_factory=lambda: Path(__file__).parent / "data" / "ai_trade.db"
    )
    # KIS — V1 호환 패턴 (KIS_ENV_TYPE virtual/real)
    kis_env_type: str = field(default_factory=lambda: os.getenv("KIS_ENV_TYPE", "virtual").lower())
    kis_real_app_key: str = field(default_factory=lambda: os.getenv("KIS_REAL_APP_KEY", "").strip())
    kis_real_app_secret: str = field(default_factory=lambda: os.getenv("KIS_REAL_APP_SECRET", "").strip())
    kis_real_account: str = field(default_factory=lambda: os.getenv("KIS_REAL_ACCOUNT", "").strip())
    kis_virtual_app_key: str = field(default_factory=lambda: os.getenv("KIS_VIRTUAL_APP_KEY", "").strip())
    kis_virtual_app_secret: str = field(default_factory=lambda: os.getenv("KIS_VIRTUAL_APP_SECRET", "").strip())
    kis_virtual_account: str = field(default_factory=lambda: os.getenv("KIS_VIRTUAL_ACCOUNT", "").strip())
    v1_token_path: Path = field(
        default_factory=lambda: Path(
            os.getenv("V1_TOKEN_PATH",
                      str(Path(__file__).parent.parent / "legacy" / "signal_v1" / "data" / "kis_token.json"))
        )
    )
    chronos_model: str = field(default_factory=lambda: os.getenv("CHRONOS_MODEL", "amazon/chronos-2"))
    stop_loss_pct: float = field(
        default_factory=lambda: float(os.getenv("STOP_LOSS_PCT", "-0.07"))
    )
    take_profit_pct: float = field(
        default_factory=lambda: float(os.getenv("TAKE_PROFIT_PCT", "0.15"))
    )
    chronos_spread_threshold: float = field(
        default_factory=lambda: float(os.getenv("CHRONOS_SPREAD_THRESHOLD", "0.6"))
    )
    asking_bid_ratio_threshold: float = field(
        default_factory=lambda: float(os.getenv("ASKING_BID_RATIO_THRESHOLD", "0.6"))
    )
    confidence_threshold: float = field(
        default_factory=lambda: float(os.getenv("CONFIDENCE_THRESHOLD", "0.7"))
    )
    min_momentum_for_buy: str = field(
        default_factory=lambda: os.getenv("MIN_MOMENTUM_FOR_BUY", "strong_up")
    )
    signal_ttl_seconds: int = field(
        default_factory=lambda: int(os.getenv("SIGNAL_TTL_SECONDS", "300"))
    )

    @property
    def kis_is_virtual(self) -> bool:
        return self.kis_env_type != "real"

    @property
    def kis_app_key(self) -> str:
        return self.kis_real_app_key if self.kis_env_type == "real" else self.kis_virtual_app_key

    @property
    def kis_app_secret(self) -> str:
        return self.kis_real_app_secret if self.kis_env_type == "real" else self.kis_virtual_app_secret

    @property
    def kis_account(self) -> str:
        return self.kis_real_account if self.kis_env_type == "real" else self.kis_virtual_account


def get_settings() -> Settings:
    return Settings()
