"""
Keyboards, and pass-throughs for the user-facing text that lives in
config.py (so it's editable from .env). Do not hardcode text here — add a
new config.py entry instead.
"""

from . import config


def buy_coin_keyboard(coin_display_name: str, buy_url: str = None) -> dict:
    """Inline button under a price/amount reply, named after the coin asked about.

    `buy_url` lets a group's own registered referral link (services/group_links)
    override the global default (config.BUY_COIN_URL).
    """
    return {
        "inline_keyboard": [
            [{"text": f"خرید {coin_display_name}", "url": buy_url or config.BUY_COIN_URL}],
        ]
    }


# --- Channel membership --------------------------------------------------------

MEMBERSHIP_REQUIRED_TEXT = config.MEMBERSHIP_REQUIRED_TEXT

MEMBERSHIP_REQUIRED_KEYBOARD = {
    "inline_keyboard": [
        [
            {"text": "عضویت در کانال", "url": config.CHANNEL_JOIN_URL},
        ],
        [
            {"text": "عضو شدم ✔️", "callback_data": "check_membership"},
        ],
    ]
}

MEMBERSHIP_CONFIRMED_TEXT = config.MEMBERSHIP_CONFIRMED_TEXT


# --- Main menu (sent right after membership is confirmed) -----------------------
#
# Row 1: price lookup. Row 2: blockchain explorer, price alert (both no-ops
# for now). Row 3: "earn" (add bot to group / referral link).

MENU_PRICE_BUTTON_TEXT = config.MENU_PRICE_BUTTON_TEXT
MENU_EXPLORER_BUTTON_TEXT = config.MENU_EXPLORER_BUTTON_TEXT
MENU_PRICE_ALERT_BUTTON_TEXT = config.MENU_PRICE_ALERT_BUTTON_TEXT
MENU_EARN_BUTTON_TEXT = config.MENU_EARN_BUTTON_TEXT

MAIN_MENU_KEYBOARD = {
    "keyboard": [
        [{"text": MENU_PRICE_BUTTON_TEXT}],
        [{"text": MENU_EXPLORER_BUTTON_TEXT}, {"text": MENU_PRICE_ALERT_BUTTON_TEXT}],
        [{"text": MENU_EARN_BUTTON_TEXT}],
    ],
    "resize_keyboard": True,
    "one_time_keyboard": False,
}

ASK_COIN_TEXT = config.ASK_COIN_TEXT


# --- Add to group / earn ---------------------------------------------------------

ADD_TO_GROUP_BUTTON_TEXT = config.ADD_TO_GROUP_BUTTON_TEXT

ADD_TO_GROUP_PROMPT_TEXT = config.ADD_TO_GROUP_PROMPT_TEXT


def add_to_group_keyboard() -> dict:
    """
    Inline button with Telegram's native "choose a group" deep link. When
    config.ADD_TO_GROUP_ADMIN_RIGHTS is set, the same link also opens the
    "make the bot an admin" screen with those rights pre-selected.
    """
    url = f"https://t.me/{config.BOT_USERNAME}?startgroup=true"
    if config.ADD_TO_GROUP_ADMIN_RIGHTS:
        url += f"&admin={config.ADD_TO_GROUP_ADMIN_RIGHTS}"
    return {
        "inline_keyboard": [
            [{"text": ADD_TO_GROUP_BUTTON_TEXT, "url": url}],
        ]
    }


# --- Group referral link ---------------------------------------------------------

def bot_added_to_group_group_message() -> str:
    """Posted in the group right after the bot joins, tagging whoever added it."""
    return config.BOT_ADDED_TO_GROUP_MESSAGE_TEXT


def ask_referral_link_text(group_title: str) -> str:
    return config.ASK_REFERRAL_LINK_TEMPLATE.format(group_title=group_title)


REFERRAL_LINK_INVALID_TEXT = config.REFERRAL_LINK_INVALID_TEXT

REFERRAL_LINK_SAVED_TEXT = config.REFERRAL_LINK_SAVED_TEXT

NOT_GROUP_ADMIN_TEXT = config.NOT_GROUP_ADMIN_TEXT


# --- Blockchain explorer -----------------------------------------------------------

EXPLORER_ASK_NETWORK_TEXT = config.EXPLORER_ASK_NETWORK_TEXT
EXPLORER_NOT_FOUND_TEXT = config.EXPLORER_NOT_FOUND_TEXT
EXPLORER_ERROR_TEXT = config.EXPLORER_ERROR_TEXT

_EXPLORER_NETWORK_LABELS = {
    "BEP20": config.EXPLORER_NETWORK_BEP20_LABEL,
    "TRC20": config.EXPLORER_NETWORK_TRC20_LABEL,
    "ERC20": config.EXPLORER_NETWORK_ERC20_LABEL,
    "BTC": config.EXPLORER_NETWORK_BTC_LABEL,
    "SOL": config.EXPLORER_NETWORK_SOL_LABEL,
}


def explorer_network_label(network: str) -> str:
    return _EXPLORER_NETWORK_LABELS.get(network, network)


def explorer_network_keyboard() -> dict:
    """
    Inline buttons to pick which network the tx hash belongs to.
    Row 1: Bitcoin alone. Row 2 & 3: the rest, two per row.
    """
    def button(network):
        return {"text": _EXPLORER_NETWORK_LABELS[network], "callback_data": f"explorer_network:{network}"}

    return {
        "inline_keyboard": [
            [button("BTC")],
            [button("BEP20"), button("TRC20")],
            [button("ERC20"), button("SOL")],
        ]
    }


def explorer_ask_hash_text(network: str) -> str:
    return config.EXPLORER_ASK_HASH_TEMPLATE.format(network=explorer_network_label(network))


def explorer_result_text(result: dict) -> str:
    return config.EXPLORER_RESULT_TEMPLATE.format(
        source_address=result.get("source_address") or "-",
        destination_address=result.get("destination_address") or "-",
        asset=result.get("asset") or "-",
        amount=result.get("amount") or "-",
        date=result.get("date") or "-",
        status=result.get("status") or "-",
        network=result.get("network") or "-",
        source=result.get("source") or "",
    )
