from fastapi import APIRouter,Depends,Query from fastapi.responses import JSONResponse from fastapi import HTTPException from db_setup import get_session from sqlalchemy.ext.asyncio import AsyncSession from pydantic import BaseModel, EmailStr from notifications.views import Confirmation,Notification from users.permissions import CurrentUser from dotenv import load_dotenv load_dotenv() router = APIRouter() class ConfirmEmailRequest(BaseModel): token: str class ConfirmEmailResend(BaseModel): email: EmailStr @router.post("/users/confirm-email") async def confirm_email(payload: ConfirmEmailRequest,session: AsyncSession = Depends(get_session)): try: service=Confirmation(session=session) data=await service.confirm(payload.token) return JSONResponse(content={"data":data,"status_code":200}) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500,detail=str(e)) @router.post("/users/confirm-email/resend") async def resend_confirm_email(payload: ConfirmEmailResend,session: AsyncSession = Depends(get_session)): try: service=Confirmation(session=session) data=await service.resend(payload.email) return JSONResponse(content={"data":data,"status_code":200}) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500,detail=str(e)) @router.get("/notifications/fetch") async def fetch_notifications( current_user: CurrentUser, unread_only: bool = Query(False), top: int | None = Query(None), skip: int = Query(0,ge=0), session: AsyncSession = Depends(get_session), ): try: service=Notification(session=session) data,total,unread=await service.get_notifications(current_user,unread_only,top,skip) return JSONResponse(content={"data":data,"total":total,"unread":unread,"status_code":200}) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500,detail=str(e)) @router.post("/notifications/{record_id}/read") async def mark_notification_read( record_id: str, current_user: CurrentUser, session: AsyncSession = Depends(get_session), ): try: service=Notification(session=session) data=await service.mark_read(record_id,current_user) return JSONResponse(content={"data":data,"total":1,"status_code":200}) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500,detail=str(e)) @router.post("/notifications/read-all") async def mark_all_notifications_read( current_user: CurrentUser, session: AsyncSession = Depends(get_session), ): try: service=Notification(session=session) data=await service.mark_all_read(current_user) return JSONResponse(content={"data":data,"status_code":200}) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500,detail=str(e)) @router.delete("/notifications/delete") async def delete_notification( current_user: CurrentUser, record_id: str = Query(...), session: AsyncSession = Depends(get_session), ): try: service=Notification(session=session) data=await service.delete_notification(record_id,current_user) return JSONResponse(content={"data":data,"status_code":200}) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500,detail=str(e))