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

Vendor pages for messaging management:
- 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_current_vendor_from_cookie_or_header, get_db
from app.modules.core.utils.page_context import get_vendor_context
from app.templates_config import templates
from app.modules.tenancy.models import User

router = APIRouter()


# ============================================================================
# MESSAGING
# ============================================================================


@router.get(
    "/{vendor_code}/messages", response_class=HTMLResponse, include_in_schema=False
)
async def vendor_messages_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_vendor_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render messages page.
    JavaScript loads conversations and messages via API.
    """
    return templates.TemplateResponse(
        "messaging/vendor/messages.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )


@router.get(
    "/{vendor_code}/messages/{conversation_id}",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def vendor_message_detail_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    conversation_id: int = Path(..., description="Conversation ID"),
    current_user: User = Depends(get_current_vendor_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render message detail page.
    Shows the full conversation thread.
    """
    return templates.TemplateResponse(
        "messaging/vendor/messages.html",
        get_vendor_context(
            request, db, current_user, vendor_code, conversation_id=conversation_id
        ),
    )


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


@router.get(
    "/{vendor_code}/email-templates",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def vendor_email_templates_page(
    request: Request,
    vendor_code: str = Path(..., description="Vendor code"),
    current_user: User = Depends(get_current_vendor_from_cookie_or_header),
    db: Session = Depends(get_db),
):
    """
    Render vendor email templates customization page.
    Allows vendors to override platform email templates.
    """
    return templates.TemplateResponse(
        "messaging/vendor/email-templates.html",
        get_vendor_context(request, db, current_user, vendor_code),
    )
