from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import Dict, Any

from app.core import get_db, get_current_active_user
from app import services
from app.schemas.order_schemas import CartItemCreate, CartItemUpdate
from app.models.user_models import User as UserModel

# Роутер для корзины
cart_router = APIRouter(prefix="/cart", tags=["Корзина"])

@cart_router.post("/items", response_model=Dict[str, Any])
async def add_to_cart_endpoint(cart_item: CartItemCreate, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.add_to_cart(db, current_user.id, cart_item)


@cart_router.put("/items/{cart_item_id}", response_model=Dict[str, Any])
async def update_cart_item_endpoint(cart_item_id: int, cart_item: CartItemUpdate, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.update_cart_item(db, current_user.id, cart_item_id, cart_item)


@cart_router.delete("/items/{cart_item_id}", response_model=Dict[str, Any])
async def remove_from_cart_endpoint(cart_item_id: int, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.remove_from_cart(db, current_user.id, cart_item_id)


@cart_router.delete("/clear", response_model=Dict[str, Any])
async def clear_cart_endpoint(current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.clear_cart(db, current_user.id)


@cart_router.get("/", response_model=Dict[str, Any])
async def get_cart_endpoint(current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.get_cart(db, current_user.id) 