from __future__ import annotations

from aiogram.types import InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder

from app.common import money
from app.models import Category, Item


def reseller_main_menu(is_admin: bool = False) -> ReplyKeyboardMarkup:
    rows = [
        [KeyboardButton(text="Catalog"), KeyboardButton(text="My purchases")],
        [KeyboardButton(text="More")],
    ]
    if is_admin:
        rows.append([KeyboardButton(text="Admin panel")])
    return ReplyKeyboardMarkup(keyboard=rows, resize_keyboard=True)


def reseller_categories_kb(categories: list[Category]) -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    for cat in categories:
        kb.button(text=f"▪ {cat.name}", callback_data=f"rcat:{cat.id}")
    kb.adjust(1)
    return kb.as_markup()


def reseller_items_kb(
    items: list[Item],
    currency: str,
    prices: dict[int, float],
    in_stock_ids: set[int],
) -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    for item in items:
        style = "success" if item.id in in_stock_ids else "danger"
        kb.button(
            text=f"{item.name} - {money(prices[item.id])} {currency}",
            callback_data=f"ritem:{item.id}",
            style=style,
        )
    kb.button(text="Back", callback_data="rcatalog")
    kb.adjust(1)
    return kb.as_markup()


def reseller_item_kb(item_id: int, category_id: int, in_stock: bool) -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    if in_stock:
        kb.button(text="Buy", callback_data=f"rbuy:{item_id}", style="success")
    kb.button(text="Back", callback_data=f"rcat:{category_id}")
    kb.adjust(1)
    return kb.as_markup()


def reseller_invoice_kb(pay_url: str, order_id: int) -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    kb.button(text="Pay", url=pay_url)
    kb.button(text="I paid - check", callback_data=f"rcheck:{order_id}")
    kb.adjust(1)
    return kb.as_markup()


def reseller_more_kb(
    support_url: str, channel_url: str, back_callback: str = "rother:menu"
) -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    kb.button(text="Google Authenticator / 2FA", callback_data="rother:2fa")
    if channel_url:
        kb.button(text="Channel", url=channel_url)
    if support_url:
        kb.button(text="Support", url=support_url)
    kb.adjust(1)
    return kb.as_markup()


def reseller_twofa_input_kb() -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    kb.button(text="Back", callback_data="rother:menu")
    return kb.as_markup()


def reseller_twofa_result_kb() -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    kb.button(text="Refresh code", callback_data="rotpgen:ref")
    kb.button(text="More", callback_data="rother:menu")
    kb.adjust(1)
    return kb.as_markup()


def reseller_admin_kb() -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    kb.button(text="Markup", callback_data="ra:markup")
    kb.button(text="Channel/subscription", callback_data="ra:channel")
    kb.button(text="Product images", callback_data="ra:images")
    kb.button(text="Broadcast", callback_data="ra:broadcast")
    kb.button(text="Balance/payout", callback_data="ra:balance")
    kb.adjust(1)
    return kb.as_markup()


def reseller_admin_back_kb() -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    kb.button(text="Back", callback_data="ra:menu")
    return kb.as_markup()


def reseller_pick_item_kb(items: list[Item]) -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    for item in items:
        kb.button(text=item.name, callback_data=f"ra:img:{item.id}")
    kb.button(text="Back", callback_data="ra:menu")
    kb.adjust(1)
    return kb.as_markup()


def reseller_payout_kb(can_payout: bool) -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    if can_payout:
        kb.button(text="Withdraw", callback_data="ra:payout")
    kb.button(text="Back", callback_data="ra:menu")
    kb.adjust(1)
    return kb.as_markup()


def reseller_sub_gate_kb(join_url: str) -> InlineKeyboardMarkup:
    kb = InlineKeyboardBuilder()
    if join_url:
        kb.button(text="Subscribe", url=join_url)
    kb.button(text="I subscribed", callback_data="rsubcheck")
    kb.adjust(1)
    return kb.as_markup()
