# app/api/v1/admin.py """Summary description .... This module provides classes and functions for: - .... - .... - .... """ import logging from typing import List, Optional from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy.orm import Session from app.api.deps import get_current_admin_user from app.core.database import get_db from app.services.admin_service import admin_service from models.api_models import ( MarketplaceImportJobResponse, ShopListResponse, UserResponse, ) from models.database_models import User router = APIRouter() logger = logging.getLogger(__name__) # Admin-only routes @router.get("/admin/users", response_model=List[UserResponse]) def get_all_users( skip: int = Query(0, ge=0), limit: int = Query(100, ge=1, le=1000), db: Session = Depends(get_db), current_admin: User = Depends(get_current_admin_user), ): """Get all users (Admin only).""" try: users = admin_service.get_all_users(db=db, skip=skip, limit=limit) return [UserResponse.model_validate(user) for user in users] except Exception as e: logger.error(f"Error getting users: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.put("/admin/users/{user_id}/status") def toggle_user_status( user_id: int, db: Session = Depends(get_db), current_admin: User = Depends(get_current_admin_user), ): """Toggle user active status (Admin only).""" try: user, message = admin_service.toggle_user_status(db, user_id, current_admin.id) return {"message": message} except HTTPException: raise except Exception as e: logger.error(f"Error toggling user {user_id} status: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.get("/admin/shops", response_model=ShopListResponse) def get_all_shops_admin( skip: int = Query(0, ge=0), limit: int = Query(100, ge=1, le=1000), db: Session = Depends(get_db), current_admin: User = Depends(get_current_admin_user), ): """Get all shops with admin view (Admin only).""" try: shops, total = admin_service.get_all_shops(db=db, skip=skip, limit=limit) return ShopListResponse(shops=shops, total=total, skip=skip, limit=limit) except Exception as e: logger.error(f"Error getting shops: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.put("/admin/shops/{shop_id}/verify") def verify_shop( shop_id: int, db: Session = Depends(get_db), current_admin: User = Depends(get_current_admin_user), ): """Verify/unverify shop (Admin only).""" try: shop, message = admin_service.verify_shop(db, shop_id) return {"message": message} except HTTPException: raise except Exception as e: logger.error(f"Error verifying shop {shop_id}: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.put("/admin/shops/{shop_id}/status") def toggle_shop_status( shop_id: int, db: Session = Depends(get_db), current_admin: User = Depends(get_current_admin_user), ): """Toggle shop active status (Admin only).""" try: shop, message = admin_service.toggle_shop_status(db, shop_id) return {"message": message} except HTTPException: raise except Exception as e: logger.error(f"Error toggling shop {shop_id} status: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.get( "/admin/marketplace-import-jobs", response_model=List[MarketplaceImportJobResponse] ) def get_all_marketplace_import_jobs( marketplace: Optional[str] = Query(None), shop_name: Optional[str] = Query(None), status: Optional[str] = Query(None), skip: int = Query(0, ge=0), limit: int = Query(100, ge=1, le=100), db: Session = Depends(get_db), current_admin: User = Depends(get_current_admin_user), ): """Get all marketplace import jobs (Admin only).""" try: return admin_service.get_marketplace_import_jobs( db=db, marketplace=marketplace, shop_name=shop_name, status=status, skip=skip, limit=limit, ) except Exception as e: logger.error(f"Error getting marketplace import jobs: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error")