# app/modules/hosting/routes/pages/public.py
"""
Hosting Public Page Routes.

Public-facing routes for POC site viewing:
- POC Viewer - Shows the Store's storefront with a HostWizard preview banner
"""

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

from app.core.database import get_db
from app.templates_config import templates

router = APIRouter()


@router.get(
    "/hosting/sites/{site_id}/preview",
    response_class=HTMLResponse,
    include_in_schema=False,
)
async def poc_site_viewer(
    request: Request,
    site_id: int = Path(..., description="Hosted Site ID"),
    db: Session = Depends(get_db),
):
    """Render POC site viewer with HostWizard preview banner."""
    from app.modules.hosting.models import HostedSite, HostedSiteStatus

    site = db.query(HostedSite).filter(HostedSite.id == site_id).first()

    # Only allow viewing for poc_ready or proposal_sent sites
    if not site or site.status not in (HostedSiteStatus.POC_READY, HostedSiteStatus.PROPOSAL_SENT):
        return HTMLResponse(content="<h1>Site not available for preview</h1>", status_code=404)

    context = {
        "request": request,
        "site": site,
        "store_url": f"/stores/{site.store.subdomain}" if site.store else "#",
    }
    return templates.TemplateResponse(
        "hosting/public/poc-viewer.html",
        context,
    )
