# Aiogram imports
from aiogram.types import InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder, ReplyKeyboardBuilder
from decouple import config
import pytz


QUESTION_CATEGORIES = [
    ("💼 Работа", "labor"),
    ("🛒 Защита прав потребителей", "consumer"),
    ("🏠 Жилье / аренда", "housing"),
    ("👪 Семья", "family"),
    ("💰 Долги / займы", "civil"),
    ("📄 Договоры", "civil"),
    ("⚖️ Суд / процесс", "procedural"),
    ("❓ Другое", "other"),
]

USER_TYPES = [
    ("Физлицо", "physical_person"),
    ("ИП", "individual_entrepreneur"),
    ("ООО", "company"),
]

CATEGORY_BY_TEXT = {text: code for text, code in QUESTION_CATEGORIES}
USER_TYPE_BY_TEXT = {text: code for text, code in USER_TYPES}
USER_TYPE_LABEL_BY_CODE = {code: text for text, code in USER_TYPES}
TZ = pytz.timezone(config("TIMEZONE"))


def get_main_menu_kb() -> ReplyKeyboardMarkup:
    builder = ReplyKeyboardBuilder()

    builder.row(KeyboardButton(text="⚖️ Задать вопрос"))
    builder.row(
        KeyboardButton(text="📚 Мои консультации"),
        KeyboardButton(text="👤 Профиль"),
    )
    builder.row(KeyboardButton(text="ℹ️ Помощь"))

    return builder.as_markup(resize_keyboard=True, is_persistent=True)


def get_categories_kb() -> ReplyKeyboardMarkup:
    builder = ReplyKeyboardBuilder()

    for text, _ in QUESTION_CATEGORIES:
        builder.add(KeyboardButton(text=text))

    builder.add(KeyboardButton(text="↩️ В меню"))
    builder.adjust(1)

    return builder.as_markup(resize_keyboard=True, is_persistent=True)


def get_back_to_menu_kb() -> ReplyKeyboardMarkup:
    builder = ReplyKeyboardBuilder()
    builder.row(KeyboardButton(text="↩️ В меню"))
    return builder.as_markup(resize_keyboard=True)


def get_profile_kb() -> ReplyKeyboardMarkup:
    builder = ReplyKeyboardBuilder()

    builder.row(KeyboardButton(text="🌍 Изменить регион"))
    builder.row(KeyboardButton(text="👔 Изменить тип пользователя"))
    builder.row(KeyboardButton(text="🗑 Удалить мои данные"))
    builder.row(KeyboardButton(text="↩️ В меню"))

    return builder.as_markup(resize_keyboard=True, is_persistent=True)


def get_user_types_kb() -> ReplyKeyboardMarkup:
    builder = ReplyKeyboardBuilder()

    for text, _ in USER_TYPES:
        builder.add(KeyboardButton(text=text))

    builder.add(KeyboardButton(text="↩️ В меню"))
    builder.adjust(1)

    return builder.as_markup(resize_keyboard=True)


def get_delete_profile_kb() -> ReplyKeyboardMarkup:
    builder = ReplyKeyboardBuilder()

    builder.row(KeyboardButton(text="✅ Да, удалить"))
    builder.row(KeyboardButton(text="↩️ В меню"))

    return builder.as_markup(resize_keyboard=True)


def get_consultations_ikb(consultations: list) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    for consultation in consultations:
        title = consultation.title or consultation.category
        updated_at = consultation.updated_at.astimezone(TZ).strftime("%d.%m.%Y")
        builder.row(
            ai_button(
                text=f"{title[:48]} — {updated_at}",
                callback_data=f"consultation:open:{consultation.id}",
            )
        )

    return builder.as_markup()


def get_consultation_actions_ikb(consultation_id: int) -> InlineKeyboardMarkup:
    builder = InlineKeyboardBuilder()

    builder.row(
        ai_button(
            text="🔁 Продолжить",
            callback_data=f"consultation:continue:{consultation_id}",
        )
    )
    builder.row(
        ai_button(
            text="🗑 Удалить",
            callback_data=f"consultation:delete:{consultation_id}",
        )
    )

    return builder.as_markup()


def ai_button(text: str, callback_data: str):
    from aiogram.types import InlineKeyboardButton

    return InlineKeyboardButton(text=text, callback_data=callback_data)
