from fastapi import APIRouter, HTTPException, Depends, status
from ..models import Order, User
from ..database import get_db
from ..auth import get_current_user
from bson import ObjectId
from typing import List

router = APIRouter(
    prefix="/orders",
    tags=["orders"]
)


@router.post("/", response_model=Order)
async def create_order(order: Order, current_user: User = Depends(get_current_user)):
  db = get_db()
  order_dict = order.dict()
  order_dict["user_id"] = str(current_user.id)
  result = db.orders.insert_one(order_dict)
  created_order = db.orders.find_one({"_id": result.inserted_id})
  return Order(**created_order)


@router.get("/", response_model=List[Order])
async def read_orders(skip: int = 0, limit: int = 10, current_user: User = Depends(get_current_user)):
  db = get_db()
  orders = list(db.orders.find(
      {"user_id": str(current_user.id)}).skip(skip).limit(limit))
  return [Order(**order) for order in orders]


@router.get("/{order_id}", response_model=Order)
async def read_order(order_id: str, current_user: User = Depends(get_current_user)):
  db = get_db()
  order = db.orders.find_one(
      {"_id": ObjectId(order_id), "user_id": str(current_user.id)})
  if order is None:
    raise HTTPException(status_code=404, detail="Order not found")
  return Order(**order)


@router.put("/{order_id}", response_model=Order)
async def update_order(order_id: str, order: Order, current_user: User = Depends(get_current_user)):
  db = get_db()
  existing_order = db.orders.find_one(
      {"_id": ObjectId(order_id), "user_id": str(current_user.id)})
  if not existing_order:
    raise HTTPException(status_code=404, detail="Order not found")
  update_data = order.dict(exclude_unset=True)
  result = db.orders.update_one(
      {"_id": ObjectId(order_id)}, {"$set": update_data})
  if result.modified_count == 1:
    updated_order = db.orders.find_one({"_id": ObjectId(order_id)})
    return Order(**updated_order)
  else:
    raise HTTPException(status_code=400, detail="Order update failed")


@router.delete("/{order_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_order(order_id: str, current_user: User = Depends(get_current_user)):
  db = get_db()
  result = db.orders.delete_one(
      {"_id": ObjectId(order_id), "user_id": str(current_user.id)})
  if result.deleted_count != 1:
    raise HTTPException(status_code=404, detail="Order not found")


@router.post("/{order_id}/process_payment")
async def process_payment(order_id: str, payment_method: str, current_user: User = Depends(get_current_user)):
  db = get_db()
  order = db.orders.find_one(
      {"_id": ObjectId(order_id), "user_id": str(current_user.id)})
  if not order:
    raise HTTPException(status_code=404, detail="Order not found")
  # Implement payment processing logic here
  # For this example, we'll just update the order status
  result = db.orders.update_one(
      {"_id": ObjectId(order_id)},
      {"$set": {"payment_status": "paid", "payment_method": payment_method}}
  )
  if result.modified_count == 1:
    return {"message": "Payment processed successfully"}
  else:
    raise HTTPException(status_code=400, detail="Payment processing failed")


@router.post("/{order_id}/apply_discount")
async def apply_discount(order_id: str, discount_percentage: float, current_user: User = Depends(get_current_user)):
  db = get_db()
  order = db.orders.find_one(
      {"_id": ObjectId(order_id), "user_id": str(current_user.id)})
  if not order:
    raise HTTPException(status_code=404, detail="Order not found")
  if discount_percentage < 0 or discount_percentage > 100:
    raise HTTPException(status_code=400, detail="Invalid discount percentage")

  new_total = order["total_amount"] * (1 - discount_percentage / 100)
  result = db.orders.update_one(
      {"_id": ObjectId(order_id)},
      {"$set": {"total_amount": new_total, "discount_applied": discount_percentage}}
  )
  if result.modified_count == 1:
    updated_order = db.orders.find_one({"_id": ObjectId(order_id)})
    return Order(**updated_order)
  else:
    raise HTTPException(status_code=400, detail="Discount application failed")
