"""Maps router for CRUD operations.""" from typing import List from uuid import UUID from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.orm import Session from app.database import get_db from app.dependencies import get_current_user, get_optional_current_user from app.models.user import User from app.schemas.map import MapCreate, MapUpdate, MapResponse from app.services import map_service from app.services import map_share_service router = APIRouter(prefix="/api/maps", tags=["maps"]) @router.get("", response_model=List[MapResponse]) async def list_user_maps( current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): """Get all maps owned by the current user.""" maps = map_service.get_user_maps(db, current_user.id) return maps @router.post("", response_model=MapResponse, status_code=status.HTTP_201_CREATED) async def create_map( map_data: MapCreate, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): """Create a new map.""" new_map = map_service.create_map(db, map_data, current_user.id) return new_map @router.get("/public", response_model=MapResponse) async def get_public_map(db: Session = Depends(get_db)): """Get the default public map (no authentication required).""" public_map = map_service.get_default_public_map(db) if not public_map: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="No public map available" ) return public_map @router.get("/shared/{token}", response_model=MapResponse) async def get_shared_map( token: str, db: Session = Depends(get_db) ): """Get a map by share token (no authentication required).""" map_obj, permission = map_share_service.get_map_by_share_token(db, token) return map_obj @router.get("/{map_id}", response_model=MapResponse) async def get_map( map_id: UUID, current_user: User = Depends(get_optional_current_user), db: Session = Depends(get_db) ): """Get a specific map by ID. Requires authentication unless it's the public map.""" map_obj = map_service.get_map_by_id(db, map_id, current_user) return map_obj @router.patch("/{map_id}", response_model=MapResponse) async def update_map( map_id: UUID, map_data: MapUpdate, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): """Update a map. Only the owner or admin can update.""" updated_map = map_service.update_map(db, map_id, map_data, current_user) return updated_map @router.delete("/{map_id}", status_code=status.HTTP_204_NO_CONTENT) async def delete_map( map_id: UUID, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): """Delete a map. Only the owner or admin can delete.""" map_service.delete_map(db, map_id, current_user) return None @router.post("/{map_id}/set-default-public", response_model=MapResponse) async def set_default_public( map_id: UUID, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): """Set a map as the default public map. Admin only.""" updated_map = map_service.set_default_public_map(db, map_id, current_user) return updated_map