"""Business event routes.

Moved from app.admin_dashboard in v4.7.2. The handlers still reuse
legacy helpers to keep this refactor behavior-preserving.
"""
from fastapi import APIRouter
import app.admin_dashboard as legacy
from app.admin_dashboard import *  # noqa: F401,F403

router = APIRouter()


@router.get("/events", response_class=HTMLResponse)
async def events_page(limit: int = 100):
    events = list_business_events(limit=limit)

    rows = ""
    for event in events:
        rows += f"""
        <tr>
            <td>{esc(event["created_at"])}</td>
            <td>{esc(event.get("event_type"))}</td>
            <td>#{esc(event.get("conversation_id"))}</td>
            <td><small>{esc(event.get("task_id"))}</small></td>
            <td>{esc(event.get("created_by"))}</td>
        </tr>
        """

    body = f"""
    <table>
        <thead>
            <tr>
                <th>Criado</th>
                <th>Evento</th>
                <th>Conversa</th>
                <th>Task</th>
                <th>Criado por</th>
            </tr>
        </thead>
        <tbody>{rows}</tbody>
    </table>
    """

    return layout("Business Events", "Eventos criados após conclusão de tarefas.", body, "events")


