from typing import List

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

from app.core.entities.update_route_request import UpdateRouteRequest
from app.core.usecases.auth_service import verify_token
from app.infrastructure.database.dependencies import get_db
from app.core.entities.delivery_order import DeliveryOrderEntity
from app.core.usecases.delivery_order_service import DeliveryOrdersService
from app.infrastructure.database.models.users import User

router = APIRouter()


@router.get("/delivery-orders", response_model=List[DeliveryOrderEntity])
def read_delivery_orders(db: Session = Depends(get_db),
                         current_user: User = Depends(verify_token)):
    service = DeliveryOrdersService(db)
    return service.get_all_delivery_orders()


@router.get("/delivery-orders/{delivery_order_id}", response_model=DeliveryOrderEntity)
def read_delivery_order(delivery_order_id: int, db: Session = Depends(get_db),
                        current_user: User = Depends(verify_token)):
    service = DeliveryOrdersService(db)
    delivery_order = service.get_delivery_order_by_id(delivery_order_id)
    if delivery_order is None:
        raise HTTPException(status_code=404, detail="Delivery order not found")
    return delivery_order


@router.get("/delivery-orders/total/{total_order_id}", response_model=List[DeliveryOrderEntity])
def read_delivery_order_by_total(total_order_id: int, db: Session = Depends(get_db),
                                 current_user: User = Depends(verify_token)):
    service = DeliveryOrdersService(db)
    delivery_order = service.get_delivery_order_by_total_order_id(total_order_id)
    if delivery_order is None:
        raise HTTPException(status_code=404, detail="Delivery order not found")
    return delivery_order


@router.post("/delivery-orders", response_model=DeliveryOrderEntity)
def create_delivery_order(delivery_order: DeliveryOrderEntity, db: Session = Depends(get_db),
                          current_user: User = Depends(verify_token)):
    service = DeliveryOrdersService(db)
    return service.create_delivery_order(delivery_order)


@router.put("/delivery-orders/{delivery_order_id}", response_model=DeliveryOrderEntity)
def update_delivery_order(delivery_order_id: int, delivery_order: DeliveryOrderEntity, db: Session = Depends(get_db),
                          current_user: User = Depends(verify_token)):
    service = DeliveryOrdersService(db)
    updated_delivery_order = service.update_delivery_order(delivery_order_id, delivery_order)
    if updated_delivery_order is None:
        raise HTTPException(status_code=404, detail="Delivery order not found")
    return updated_delivery_order


@router.delete("/delivery-orders/{delivery_order_id}", response_model=bool)
def delete_delivery_order(delivery_order_id: int, db: Session = Depends(get_db),
                          current_user: User = Depends(verify_token)):
    service = DeliveryOrdersService(db)
    success = service.delete_delivery_order(delivery_order_id)
    if not success:
        raise HTTPException(status_code=404, detail="Delivery order not found")
    return success


@router.put("/delivery-orders/{delivery_order_id}/route", response_model=DeliveryOrderEntity)
def update_delivery_order_route(
        delivery_order_id: int,
        request: UpdateRouteRequest,
        db: Session = Depends(get_db),
        current_user: User = Depends(verify_token)
):
    service = DeliveryOrdersService(db)
    updated_delivery_order = service.update_delivery_order_route(
        delivery_order_id,
        request.estimated_route_time_in_minutes,
        request.route
    )

    if updated_delivery_order is None:
        raise HTTPException(status_code=404, detail="Delivery order not found")
    return updated_delivery_order
