from typing import List, Optional from fastapi import APIRouter, Depends, HTTPException, Query, BackgroundTasks from sqlalchemy.orm import Session from app.core.database import get_db from app.api.deps import get_current_user, get_user_shop from app.services.shop_service import shop_service from app.tasks.background_tasks import process_marketplace_import from middleware.decorators import rate_limit from models.api_models import MarketplaceImportJobResponse, MarketplaceImportRequest, ShopResponse, ShopCreate, \ ShopListResponse, ShopProductResponse, ShopProductCreate from models.database_models import User, MarketplaceImportJob, Shop, Product, ShopProduct from datetime import datetime import logging router = APIRouter() logger = logging.getLogger(__name__) # Shop Management Routes @router.post("/shop", response_model=ShopResponse) def create_shop( shop_data: ShopCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user) ): """Create a new shop (Protected)""" try: shop = shop_service.create_shop(db=db, shop_data=shop_data, current_user=current_user) return ShopResponse.model_validate(shop) except HTTPException: raise except Exception as e: logger.error(f"Error creating shop: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.get("/shop", response_model=ShopListResponse) def get_shops( skip: int = Query(0, ge=0), limit: int = Query(100, ge=1, le=1000), active_only: bool = Query(True), verified_only: bool = Query(False), db: Session = Depends(get_db), current_user: User = Depends(get_current_user) ): """Get shops with filtering (Protected)""" try: shops, total = shop_service.get_shops( db=db, current_user=current_user, skip=skip, limit=limit, active_only=active_only, verified_only=verified_only ) return ShopListResponse( shops=shops, total=total, skip=skip, limit=limit ) except HTTPException: raise except Exception as e: logger.error(f"Error getting shops: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.get("/shop/{shop_code}", response_model=ShopResponse) def get_shop(shop_code: str, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)): """Get shop details (Protected)""" try: shop = shop_service.get_shop_by_code(db=db, shop_code=shop_code, current_user=current_user) return ShopResponse.model_validate(shop) except HTTPException: raise except Exception as e: logger.error(f"Error getting shop {shop_code}: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") # Shop Product Management @router.post("/shop/{shop_code}/products", response_model=ShopProductResponse) def add_product_to_shop( shop_code: str, shop_product: ShopProductCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user) ): """Add existing product to shop catalog with shop-specific settings (Protected)""" try: # Get and verify shop (using existing dependency) shop = get_user_shop(shop_code, current_user, db) # Add product to shop new_shop_product = shop_service.add_product_to_shop( db=db, shop=shop, shop_product=shop_product ) # Return with product details response = ShopProductResponse.model_validate(new_shop_product) response.product = new_shop_product.product return response except HTTPException: raise except Exception as e: logger.error(f"Error adding product to shop {shop_code}: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error") @router.get("/shop/{shop_code}/products") def get_shop_products( shop_code: str, skip: int = Query(0, ge=0), limit: int = Query(100, ge=1, le=1000), active_only: bool = Query(True), featured_only: bool = Query(False), db: Session = Depends(get_db), current_user: User = Depends(get_current_user) ): """Get products in shop catalog (Protected)""" try: # Get shop shop = shop_service.get_shop_by_code(db=db, shop_code=shop_code, current_user=current_user) # Get shop products shop_products, total = shop_service.get_shop_products( db=db, shop=shop, current_user=current_user, skip=skip, limit=limit, active_only=active_only, featured_only=featured_only ) # Format response products = [] for sp in shop_products: product_response = ShopProductResponse.model_validate(sp) product_response.product = sp.product products.append(product_response) return { "products": products, "total": total, "skip": skip, "limit": limit, "shop": ShopResponse.model_validate(shop) } except HTTPException: raise except Exception as e: logger.error(f"Error getting products for shop {shop_code}: {str(e)}") raise HTTPException(status_code=500, detail="Internal server error")