# retoor <retoor@molodetz.nl>

import logging
from fastapi import APIRouter, Request
from fastapi.responses import PlainTextResponse, Response
from devplacepy.seo import make_sitemap, site_url

logger = logging.getLogger(__name__)
router = APIRouter()


@router.get("/robots.txt", response_class=PlainTextResponse)
async def robots_txt(request: Request):
    base = site_url(request)
    return PlainTextResponse(
        f"""User-agent: *
Disallow: /auth/
Disallow: /messages/
Disallow: /notifications/
Disallow: /votes/
Disallow: /avatar/
Disallow: /follow/
Disallow: /admin/
Disallow: /uploads/
Disallow: /reports/mine
Disallow: /profile/*/delete
Disallow: /*?tab=
Disallow: /*?sort=
Allow: /static/

Sitemap: {base}/sitemap.xml
""",
        headers={"Cache-Control": "public, max-age=3600"},
    )


@router.get("/sitemap.xml")
async def sitemap_xml(request: Request):
    base = site_url(request)
    xml = make_sitemap(base)
    return Response(
        content=xml,
        media_type="application/xml",
        headers={"Cache-Control": "public, max-age=3600"},
    )
