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, get_current_admin_user
from app import services
from app.schemas.review_schemas import ReviewCreate, ReviewUpdate
from app.models.user_models import User as UserModel

# Роутер для отзывов
review_router = APIRouter(prefix="/reviews", tags=["Отзывы"])

@review_router.post("/", response_model=Dict[str, Any])
async def create_review_endpoint(review: ReviewCreate, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.create_review(db, current_user.id, review)


@review_router.put("/{review_id}", response_model=Dict[str, Any])
async def update_review_endpoint(review_id: int, review: ReviewUpdate, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.update_review(db, current_user.id, review_id, review, current_user.is_admin)


@review_router.delete("/{review_id}", response_model=Dict[str, Any])
async def delete_review_endpoint(review_id: int, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.delete_review(db, current_user.id, review_id, current_user.is_admin)


@review_router.post("/{review_id}/approve", response_model=Dict[str, Any])
async def approve_review_endpoint(review_id: int, current_user: UserModel = Depends(get_current_admin_user), db: Session = Depends(get_db)):
    return services.approve_review(db, review_id)


@review_router.get("/products/{product_id}", response_model=Dict[str, Any])
async def get_product_reviews_endpoint(product_id: int, skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
    return services.get_product_reviews(db, product_id, skip, limit) 