from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.utils.keyboard import InlineKeyboardBuilder

def admin_menu() -> InlineKeyboardMarkup:
    markup = InlineKeyboardMarkup(inline_keyboard=[
        [
            InlineKeyboardButton(text='🛒Платежные системы', callback_data='payments_systems'),
            InlineKeyboardButton(text='🎁Промокоды', callback_data='promo_list')
        ],
        [
            InlineKeyboardButton(text='✏️Редактировать цены', callback_data='edit_price'),
            InlineKeyboardButton(text='👥Рассылка', callback_data='mailing')
        ],
        [
            InlineKeyboardButton(text='🔗Гипер-ссылки', callback_data='hyperlink')
        ],
        [
            InlineKeyboardButton(text='📊Статистика', callback_data='stats')
        ]
    ])
    return markup

def admin_payments_menu(data_payments: dict) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    for payment in data_payments:
        builder.add(InlineKeyboardButton(text=payment["system"], callback_data=f"edit_payment_{payment['id']}"))

    builder.adjust(2)

    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()

def payments_status_menu(payment_system_id: int, is_active: bool) -> InlineKeyboardMarkup:
    inline_keyboard = []

    if is_active:
        inline_keyboard.append([
            InlineKeyboardButton(text='🔴Выключить', callback_data=f'status_{payment_system_id}_off')
        ])

    if not is_active:
        inline_keyboard.append([
            InlineKeyboardButton(text='🟢Включить', callback_data=f'status_{payment_system_id}_on')
        ])

    inline_keyboard.append([
        InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back')
    ])

    markup = InlineKeyboardMarkup(inline_keyboard=inline_keyboard)
    return markup


def admin_back_menu() -> InlineKeyboardMarkup:
    markup = InlineKeyboardMarkup(inline_keyboard=[
        [
            InlineKeyboardButton(text='↪️Вернуться в меню', callback_data='admin_back')
        ]
    ])
    return markup


def hyperlink_menu() -> InlineKeyboardMarkup:
    markup = InlineKeyboardMarkup(inline_keyboard=[
        [
            InlineKeyboardButton(text='🔗Активные ссылки', callback_data='active_hyperlink'),
            InlineKeyboardButton(text='✒️Создать ссылку', callback_data='create_hyperlink')
        ]
    ])
    return markup


def active_hyperlink_menu(data_hyperlinks: dict) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    if len(data_hyperlinks) == 0:
        builder.row(InlineKeyboardButton(text="❌Активных ссылок нет❌", callback_data='empty'))

    for hyperlink in data_hyperlinks:
        builder.add(InlineKeyboardButton(text=f'{hyperlink["link"]} ({hyperlink["counts"]}👁)', callback_data=f"edit_hyperlink_{hyperlink['id']}"))

    builder.adjust(1)

    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()


def edit_hyperlink_menu(hyperlink_id: str) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    builder.add(InlineKeyboardButton(text='📊Выгрузить пользователей', callback_data=f"users_hyperlink_{hyperlink_id}"))
    builder.add(InlineKeyboardButton(text=f'🗑Удалить', callback_data=f"delete_hyperlink_{hyperlink_id}"))
    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()

def edit_price_products_menu(data_products: dict) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    for product in data_products:
        builder.add(InlineKeyboardButton(text=f"#{product['id']} {product['name']}", callback_data=f"edit_product_{product['id']}"))

    builder.adjust(1)

    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()

def edit_price_variants_menu(data_variants: dict) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    for variant in data_variants:
        builder.add(InlineKeyboardButton(text=variant["name"], callback_data=f"edit_variant_{variant['id']}"))

    builder.adjust(1)

    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()

def back_variants_menu(product_id: int) -> InlineKeyboardMarkup:
    markup = InlineKeyboardMarkup(inline_keyboard=[
        [
            InlineKeyboardButton(text='↪️Вернуться к товарам', callback_data=f'back_variants_{product_id}')
        ]
    ])
    return markup

def edit_variant_menu(variant_id: int, product_id: int) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    builder.add(InlineKeyboardButton(text='✏️Редактировать цену', callback_data=f"change_price_{variant_id}"))
    builder.adjust(1)

    builder.row(InlineKeyboardButton(text='↪️Вернуться к товарам', callback_data=f'back_variants_{product_id}'))
    return builder.as_markup()

def promo_actions_menu() -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    builder.add(InlineKeyboardButton(text='🎁Создать промокод', callback_data="create_promo"))
    builder.add(InlineKeyboardButton(text='🗂Список промокодов', callback_data="promo_code_list"))
    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()

def create_promo_menu() -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    builder.add(InlineKeyboardButton(text='🛒На скидку', callback_data="create_discount"))
    builder.add(InlineKeyboardButton(text='🌀На трафик', callback_data="create_traffic"))
    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()

def create_promo_traffic_products_menu(data_products: dict) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    for product in data_products:
        builder.add(InlineKeyboardButton(text=f"#{product['id']} {product['name']}", callback_data=f"traffic_promo_{product['id']}"))

    builder.adjust(1)

    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()

def edit_mailing_menu() -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    builder.add(InlineKeyboardButton(text='🟢Отправить', callback_data="send_mailing"))
    builder.add(InlineKeyboardButton(text='✏Редактировать', callback_data="edit_mailing"))
    builder.row(InlineKeyboardButton(text="↪️Вернуться в меню", callback_data='admin_back'))
    return builder.as_markup()