from django.urls import include, path
from rest_framework.routers import DefaultRouter

from . import views

app_name = 'lifey_api'

# Mounted at `api/` in backend/urls.py. The feature routes that land here are
# fixed by what each `features/<x>/data/api.js` already calls — note that they
# are NOT all `/api/<feature>/`:
#
#   tasks       /api/tasks/
#   habits      /api/habits/
#   goals       /api/goals/
#   calendar    /api/events/                    <- not /api/calendar/
#   notes       /api/notes/
#   budget      /api/budget/transactions/
#   bookshelf   /api/books/
#   focus       /api/focus-sessions/
#   capture     /api/captures/                  <- global, not page-scoped
#
# Changing one of these is a client change, not a server preference.

# Phase 2's two routers. `spaces` carries the whole tree, so `pages` exists for
# writes and for the two actions a nested list cannot express (settings merge
# and copy) — not as a second way to read the tree.
router = DefaultRouter()
router.register('spaces', views.SpaceViewSet, basename='space')
router.register('pages', views.PageViewSet, basename='page')
router.register('tasks', views.TaskViewSet, basename='task')
router.register('goals', views.GoalViewSet, basename='goal')
router.register('events', views.EventViewSet, basename='event')
router.register('notes', views.NoteViewSet, basename='note')
router.register('habits', views.HabitViewSet, basename='habit')
# Two segments, because that is what the client calls — see the map above.
router.register('budget/transactions', views.TransactionViewSet, basename='transaction')
router.register('books', views.BookViewSet, basename='book')
router.register('focus-sessions', views.FocusSessionViewSet, basename='focus-session')
# The one list that is NOT page-scoped, and the one with no `bulk`.
router.register('captures', views.CaptureViewSet, basename='capture')

urlpatterns = [
    path('auth/ping/', views.ping, name='ping'),

    # THE OPEN LIBRARY PROXY — the browser's stand-in for the desktop app's
    # `window.lifey.books` bridge. Deliberately NOT under `books/`: the router
    # registers `books/<pk>/` and a detail lookup would happily match a word,
    # so a sibling prefix is one fewer thing to get right by ordering.
    path('book-lookup/search/', views.book_search, name='book-search'),
    path('book-lookup/cover/<int:cover_id>/', views.book_cover, name='book-cover'),
    path('', include(router.urls)),
]
