import os
from dataclasses import dataclass
from dotenv import load_dotenv


@dataclass
class Config:
    # Core
    bot_token: str
    postgres_dsn: str
    redis_url: str

    news_channel_id: str
    news_channel_link: str

    welcome_image_url: str
    welcome_description: str

    profile_offer_url: str
    profile_privacy_url: str

    support_url: str
    admin_id: str

    # === Old providers (left for backward compatibility) ===
    cryptomus_api_key: str = ""
    cryptomus_merchant_id: str = ""
    cryptomus_webhook_secret: str = ""

    mulenpay_api_key: str = ""
    mulenpay_secret_key: str = ""
    mulenpay_shop_id: str = ""
    mulenpay_merchant_id: str = ""
    mulenpay_webhook_secret: str = ""
    mulenpay_callback_url: str = ""

    heleket_api_key: str = ""
    heleket_merchant_id: str = ""
    heleket_callback_url: str = ""

    # Fragment
    fragment_api_key: str = ""
    fragment_shop_id: str = ""
    fragment_phone_number: str = ""
    fragment_mnemonics: str = ""
    fragment_jwt_token: str = ""

    # === CryptoBot (Crypto Pay API) ===
    cryptopay_token: str = ""
    # recommended: random string to put into webhook URL path
    cryptopay_webhook_path_secret: str = ""

    # === TonConnect deposits ===
    public_base_url: str = ""  # https://your-domain.tld
    ton_receiver_address: str = ""  # your TON wallet address (user-friendly)
    # TON Center API is used only to check incoming transactions.
    toncenter_api_key: str = ""
    toncenter_base_url: str = "https://toncenter.com/api/v2"


def load_config() -> Config:
    load_dotenv()

    welcome_desc = os.getenv(
        "WELCOME_DESCRIPTION",
        "<b>Добро пожаловать!</b>\n\nЗдесь вы можете купить звезды и премиум, пополнить баланс, воспользоваться реферальной системой и многое другое.",
    ).replace("\\n", "\n")

    return Config(
        bot_token=os.getenv("BOT_TOKEN", ""),
        postgres_dsn=os.getenv("POSTGRES_DSN", ""),
        redis_url=os.getenv("REDIS_URL", "redis://127.0.0.1:6379/2"),

        news_channel_id=os.getenv("NEWS_CHANNEL_ID", ""),
        news_channel_link=os.getenv("NEWS_CHANNEL_LINK", ""),

        welcome_image_url=os.getenv("WELCOME_IMAGE_URL", "https://placehold.co/400x200/png"),
        welcome_description=welcome_desc,

        profile_offer_url=os.getenv("PROFILE_OFFER_URL", "https://example.com/offer"),
        profile_privacy_url=os.getenv("PROFILE_PRIVACY_URL", "https://example.com/privacy"),

        support_url=os.getenv("SUPPORT_URL", ""),
        admin_id=os.getenv("ADMIN_ID", ""),

        cryptomus_api_key=os.getenv("CRYPTOMUS_API_KEY", ""),
        cryptomus_merchant_id=os.getenv("CRYPTOMUS_MERCHANT_ID", ""),
        cryptomus_webhook_secret=os.getenv("CRYPTOMUS_WEBHOOK_SECRET", ""),

        mulenpay_api_key=os.getenv("MULENPAY_API_KEY", ""),
        mulenpay_secret_key=os.getenv("MULENPAY_SECRET_KEY", ""),
        mulenpay_shop_id=os.getenv("MULENPAY_SHOP_ID", ""),
        mulenpay_merchant_id=os.getenv("MULENPAY_MERCHANT_ID", ""),
        mulenpay_webhook_secret=os.getenv("MULENPAY_WEBHOOK_SECRET", os.getenv("MULENPAY_SECRET_KEY", "")),
        mulenpay_callback_url=os.getenv("MULENPAY_CALLBACK_URL", ""),

        heleket_api_key=os.getenv("HELEKET_API_KEY", ""),
        heleket_merchant_id=os.getenv("HELEKET_MERCHANT_ID", ""),
        heleket_callback_url=os.getenv("HELEKET_CALLBACK_URL", ""),

        fragment_api_key=os.getenv("FRAGMENT_API_KEY", ""),
        fragment_shop_id=os.getenv("FRAGMENT_SHOP_ID", ""),
        fragment_phone_number=os.getenv("FRAGMENT_PHONE_NUMBER", ""),
        fragment_mnemonics=os.getenv("FRAGMENT_MNEMONICS", ""),
        fragment_jwt_token=os.getenv("FRAGMENT_JWT_TOKEN", ""),

        cryptopay_token=os.getenv("CRYPTOPAY_TOKEN", ""),
        cryptopay_webhook_path_secret=os.getenv("CRYPTOPAY_WEBHOOK_PATH_SECRET", ""),

        public_base_url=os.getenv("PUBLIC_BASE_URL", "").rstrip("/"),
        ton_receiver_address=os.getenv("TON_RECEIVER_ADDRESS", ""),
        toncenter_api_key=os.getenv("TONCENTER_API_KEY", ""),
        toncenter_base_url=os.getenv("TONCENTER_BASE_URL", "https://toncenter.com/api/v2").rstrip("/"),
    )
