from typing import List

from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session

from app.core.usecases.auth_service import verify_token
from app.infrastructure.database.dependencies import get_db
from app.core.entities.delivery_accessory import DeliveryAccessoryEntity
from app.core.usecases.delivery_accessory_service import DeliveryAccessoriesService
from app.infrastructure.database.models.users import User

router = APIRouter()


@router.get("/delivery-accessories", response_model=List[DeliveryAccessoryEntity])
def read_delivery_accessories(db: Session = Depends(get_db),
                              current_user: User = Depends(verify_token)):
    service = DeliveryAccessoriesService(db)
    return service.get_all_delivery_accessories()


@router.get("/delivery-orders/{delivery_order_id}/accessories", response_model=List[DeliveryAccessoryEntity])
def get_delivery_accessories_by_order(delivery_order_id: int, db: Session = Depends(get_db),
                                      current_user: User = Depends(verify_token)):
    service = DeliveryAccessoriesService(db)
    return service.get_all_delivery_accessories_by_order(delivery_order_id)


@router.get("/delivery-accessories/{delivery_accessory_id}", response_model=DeliveryAccessoryEntity)
def read_delivery_accessory(delivery_accessory_id: int, db: Session = Depends(get_db),
                            current_user: User = Depends(verify_token)):
    service = DeliveryAccessoriesService(db)
    delivery_accessory = service.get_delivery_accessory_by_id(delivery_accessory_id)
    if delivery_accessory is None:
        raise HTTPException(status_code=404, detail="Delivery accessory not found")
    return delivery_accessory


@router.post("/delivery-accessories", response_model=DeliveryAccessoryEntity)
def create_delivery_accessory(delivery_accessory: DeliveryAccessoryEntity, db: Session = Depends(get_db),
                              current_user: User = Depends(verify_token)):
    service = DeliveryAccessoriesService(db)
    return service.create_delivery_accessory(delivery_accessory)


@router.put("/delivery-accessories/{delivery_accessory_id}", response_model=DeliveryAccessoryEntity)
def update_delivery_accessory(delivery_accessory_id: int, delivery_accessory: DeliveryAccessoryEntity,
                              db: Session = Depends(get_db),
                              current_user: User = Depends(verify_token)):
    service = DeliveryAccessoriesService(db)
    updated_delivery_accessory = service.update_delivery_accessory(delivery_accessory_id, delivery_accessory)
    if updated_delivery_accessory is None:
        raise HTTPException(status_code=404, detail="Delivery accessory not found")
    return updated_delivery_accessory


@router.delete("/delivery-accessories/{delivery_accessory_id}", response_model=bool)
def delete_delivery_accessory(delivery_accessory_id: int, db: Session = Depends(get_db),
                              current_user: User = Depends(verify_token)):
    service = DeliveryAccessoriesService(db)
    success = service.delete_delivery_accessory(delivery_accessory_id)
    if not success:
        raise HTTPException(status_code=404, detail="Delivery accessory not found")
    return success
