from fastapi import APIRouter, Request, HTTPException, Depends from sqlalchemy.ext.asyncio import AsyncSession from typing import Annotated from app.database import get_db from app.services.whatsapp import WhatsAppService from app.services.customer import CustomerService from app.services.translation import TranslationService from app.core.security import decode_token from app.api.v1.deps import get_current_user_id from app.config import settings router = APIRouter() @router.get("/webhook") async def verify_webhook( hub_mode: str = None, hub_verify_token: str = None, hub_challenge: str = None, ): svc = WhatsAppService() result = svc.verify_webhook(hub_mode, hub_verify_token, hub_challenge) if result: return int(result) raise HTTPException(status_code=403, detail="Verification failed") @router.post("/webhook") async def handle_webhook(request: Request, db: Annotated[AsyncSession, Depends(get_db)] = None): svc = WhatsAppService() body = await request.json() msg_data = svc.parse_webhook(body) if not msg_data: return {"status": "ok"} # TODO: Route to correct user based on WhatsApp number # For MVP, handle as generic incoming message return {"status": "ok", "message": "received"} @router.post("/send") async def send_message( data: dict, user_id: str = Depends(get_current_user_id), ): text = data.get("text") to = data.get("to") if not text or not to: raise HTTPException(status_code=400, detail="text and to are required") svc = WhatsAppService() sent = await svc.send_text(to, text) if not sent: raise HTTPException(status_code=500, detail="Failed to send WhatsApp message") return {"status": "sent", "to": to} @router.get("/qr") async def get_qr(): return {"message": "WhatsApp QR login not available via API. Use WhatsApp Cloud API instead."}