from ninja import Router, Schema from typing import List, Optional from core.models import Library, AppUser, MediaCollection from django.shortcuts import get_object_or_404 router = Router(tags=["library"]) collections_router = Router(tags=["collections"]) class LibrarySchema(Schema): id: int name: str description: Optional[str] = None visibility: str owner_user_id: int class LibraryCreateSchema(Schema): name: str description: Optional[str] = None visibility: Optional[str] = 'private' owner_user_id: int # In a real app with auth, this would come from request.user class MediaCollectionSchema(Schema): id: int name: str library_id: int collection_type: str @router.get("/", response=List[LibrarySchema]) def list_libraries(request): return Library.objects.all() @router.get("/{library_id}", response=LibrarySchema) def get_library(request, library_id: int): return get_object_or_404(Library, id=library_id) @router.post("/", response={201: LibrarySchema}) def create_library(request, payload: LibraryCreateSchema): owner = get_object_or_404(AppUser, id=payload.owner_user_id) library = Library.objects.create( owner_user=owner, name=payload.name, description=payload.description, visibility=payload.visibility ) return 201, library @collections_router.get("/", response=List[MediaCollectionSchema]) def list_collections(request): """List all MediaCollections across all libraries.""" return MediaCollection.objects.select_related('library').all()