"""Telegram command handlers: /start, /help, /search, /recent, /stats, /lang."""
from __future__ import annotations

import structlog
from telegram import Update
from telegram.ext import ContextTypes

from app.bot.messages import t
from app.bot.session import SessionManager

log = structlog.get_logger(__name__)


async def handle_start(
    update: Update,
    context: ContextTypes.DEFAULT_TYPE,
    session: SessionManager,
) -> None:
    if not update.effective_user or not update.message:
        return
    user = update.effective_user
    lang = await session.get_field(user.id, "lang", "ar")
    log.info("bot.command.start", telegram_id=user.id, username=user.username)
    await update.message.reply_text(t("welcome", lang))


async def handle_help(
    update: Update,
    context: ContextTypes.DEFAULT_TYPE,
    session: SessionManager,
) -> None:
    if not update.effective_user or not update.message:
        return
    lang = await session.get_field(update.effective_user.id, "lang", "ar")
    await update.message.reply_text(t("help", lang))


async def handle_lang(
    update: Update,
    context: ContextTypes.DEFAULT_TYPE,
    session: SessionManager,
) -> None:
    if not update.effective_user or not update.message:
        return
    uid = update.effective_user.id
    current = await session.get_field(uid, "lang", "ar")
    new_lang = "en" if current == "ar" else "ar"
    await session.update(uid, lang=new_lang)
    key = "lang_switched_en" if new_lang == "en" else "lang_switched_ar"
    await update.message.reply_text(t(key, new_lang))
    log.info("bot.command.lang", telegram_id=uid, lang=new_lang)


async def handle_search_command(
    update: Update,
    context: ContextTypes.DEFAULT_TYPE,
    session: SessionManager,
) -> None:
    if not update.effective_user or not update.message:
        return
    uid = update.effective_user.id
    lang = await session.get_field(uid, "lang", "ar")
    await session.update(uid, state="awaiting_search_query")
    await update.message.reply_text(t("search_prompt", lang))


async def handle_recent(
    update: Update,
    context: ContextTypes.DEFAULT_TYPE,
    session: SessionManager,
    db_session: object,  # AsyncSession injected at dispatcher level
) -> None:
    if not update.effective_user or not update.message:
        return
    lang = await session.get_field(update.effective_user.id, "lang", "ar")
    # Actual DB query handled in dispatcher; placeholder response here
    await update.message.reply_text(t("recent_empty", lang))


async def handle_stats(
    update: Update,
    context: ContextTypes.DEFAULT_TYPE,
    session: SessionManager,
) -> None:
    if not update.effective_user or not update.message:
        return
    lang = await session.get_field(update.effective_user.id, "lang", "ar")
    # Stub — real stats populated by dispatcher
    await update.message.reply_text(t("stats", lang, total=0, videos=0, links=0, this_week=0, top_tag="—"))


async def handle_unknown_command(
    update: Update,
    context: ContextTypes.DEFAULT_TYPE,
    session: SessionManager,
) -> None:
    if not update.effective_user or not update.message:
        return
    lang = await session.get_field(update.effective_user.id, "lang", "ar")
    await update.message.reply_text(t("unknown_command", lang))
