from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton

from config import REGIONS_PER_PAGE


def create_region_keyboard(regions, page, page_size=REGIONS_PER_PAGE):
    markup = InlineKeyboardMarkup(row_width=2)

    start = page * page_size
    end = start + page_size
    page_regions = regions[start:end]

    for region in page_regions:
        region_id_str = f"{region['id']:02d}"  # 2 цифры, с ведущими нулями
        button_text = f"{region['id']:02d}: {region['name']}"
        button = InlineKeyboardButton(text=button_text, callback_data=f"region_{region_id_str}")
        markup.add(button)

    # Пагинация
    navigation_buttons = []
    if page > 0:
        navigation_buttons.append(InlineKeyboardButton("⬅️", callback_data=f"regions_page_{page - 1}"))
    if end < len(regions):
        navigation_buttons.append(InlineKeyboardButton("➡️", callback_data=f"regions_page_{page + 1}"))
    if navigation_buttons:
        markup.add(*navigation_buttons)

    # Кнопка отмены в самый низ
    cancel_button = InlineKeyboardButton("Отмена", callback_data="cancel_input")
    markup.add(cancel_button)

    return markup

def create_group_keyboard(groups, region_id):
    markup = InlineKeyboardMarkup()
    for group in groups:
        markup.add(InlineKeyboardButton(text=group['name'], callback_data=f"group_{group['groupid']}"))
    markup.add(InlineKeyboardButton(text="Все группы региона\n(Долгое выполнение)", callback_data=f"all_groups_{region_id}"))
    return markup
