# app/modules/messaging/routes/pages/admin.py
"""
Messaging Admin Page Routes (HTML rendering).

Admin pages for messaging management:
- Notifications
- Messages list
- Conversation detail
- Email templates
"""

from fastapi import APIRouter, Depends, Path, Request
from fastapi.responses import HTMLResponse
from sqlalchemy.orm import Session

from app.api.deps import get_db, require_menu_access
from app.modules.core.utils.page_context import get_admin_context
from app.modules.enums import FrontendType
from app.modules.tenancy.models import User
from app.templates_config import templates

router = APIRouter()


# ============================================================================
# NOTIFICATIONS ROUTES
# ============================================================================


@router.get("/notifications", response_class=HTMLResponse, include_in_schema=False)
async def admin_notifications_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("notifications", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render notifications management page.
    Shows all admin notifications and platform alerts.
    """
    return templates.TemplateResponse(
        "messaging/admin/notifications.html",
        get_admin_context(request, db, current_user),
    )


# ============================================================================
# MESSAGING ROUTES
# ============================================================================


@router.get("/messages", response_class=HTMLResponse, include_in_schema=False)
async def admin_messages_page(
    request: Request,
    current_user: User = Depends(require_menu_access("messages", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """
    Render messaging page.
    Shows all conversations (admin_store and admin_customer channels).
    """
    return templates.TemplateResponse(
        "messaging/admin/messages.html",
        get_admin_context(request, db, current_user),
    )


@router.get(
    "/messages/{conversation_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def admin_conversation_detail_page(
    request: Request,
    conversation_id: int = Path(..., description="Conversation ID"),
    current_user: User = Depends(require_menu_access("messages", FrontendType.ADMIN)),
    db: Session = Depends(get_db),
):
    """
    Render conversation detail page.
    Shows the full conversation thread with messages.
    """
    return templates.TemplateResponse(
        "messaging/admin/messages.html",
        get_admin_context(request, db, current_user, conversation_id=conversation_id),
    )


# ============================================================================
# EMAIL TEMPLATES ROUTES
# ============================================================================


@router.get("/email-templates", response_class=HTMLResponse, include_in_schema=False)
async def admin_email_templates_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("email-templates", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render email templates management page.
    Shows all platform email templates with edit capabilities.
    """
    return templates.TemplateResponse(
        "messaging/admin/email-templates.html",
        get_admin_context(request, db, current_user),
    )


# ============================================================================
# EMAIL LOGS (AUDIT) ROUTES
# ============================================================================


@router.get("/email-logs", response_class=HTMLResponse, include_in_schema=False)
async def admin_email_logs_page(
    request: Request,
    current_user: User = Depends(
        require_menu_access("email-logs", FrontendType.ADMIN)
    ),
    db: Session = Depends(get_db),
):
    """
    Render email logs audit page.
    Shows all emails sent through the platform with filtering and detail view.
    """
    return templates.TemplateResponse(
        "messaging/admin/email-logs.html",
        get_admin_context(request, db, current_user),
    )
