from django.conf import settings
from django.contrib import admin
from django.urls import path, re_path, include
from django.views.static import serve as static_serve
from core.views import csrf_token, pwa_index, pwa_service_worker
from users.api_views import CookieTokenRefreshView

urlpatterns = [
    path('admin/', admin.site.urls),

    # The PWA — docs/pwa-boundary.md §2, backend/CLAUDE.md "Serving the PWA".
    # `index.html` and `sw.js` are views (headers neither a static file nor a
    # <meta> tag can carry); everything else the build references
    # (`/app/assets/…`, `/app/icons/…`, `/app/manifest.webmanifest`) is a
    # plain file under `PWA_DIR`, so one `django.views.static.serve` route
    # covers all three rather than one route per subdirectory.
    path('app/', pwa_index, name='pwa-index'),
    path('app/sw.js', pwa_service_worker, name='pwa-service-worker'),
    re_path(
        r'^app/(?P<path>assets/.+|icons/.+|manifest\.webmanifest)$',
        static_serve,
        {'document_root': settings.PWA_DIR},
        name='pwa-static',
    ),

    # The browser half of desktop sign-in: HTML pages with a session cookie,
    # not JSON. Deliberately not under api/.
    path('auth/', include('users.browser_urls', namespace='auth-browser')),

    # THIS EXACT PATH is what lib/apiClient.js's refresh() calls. Moving it is
    # a client change.
    #
    # Not the stock `TokenRefreshView`: a browser client holds its refresh
    # token in an httpOnly cookie it cannot read, so it has no body to send.
    # A request that DOES carry a body never reaches the cookie branch, so the
    # desktop contract is unchanged — see users/cookies.py.
    path('api/token/refresh/', CookieTokenRefreshView.as_view(), name='token-refresh'),

    path('api/csrf/', csrf_token, name='csrf-token'),
    path('api/auth/', include('users.urls', namespace='users')),
    path('api/waitlist/', include('waitlist.urls', namespace='waitlist')),
    path('api/newsletter/', include('newsletter.urls', namespace='newsletter')),
    path('api/', include('lifey_api.urls', namespace='lifey_api')),
]
