
    KCj              
          d Z ddlmZmZ ddlmZmZmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZmZ  e       Z G d
 de      Zej3                  de      d ee       ee      fdedede
defd       Zej9                  de       ee       ee      fdedede
defd       Zej9                  dd       ee       ee      fdede
ddfd       Zy)u?  
Notification endpoints — the in-app alert bell (module 1.1.11).

GET   /notifications              list, most recent first, with unread count
PATCH /notifications/{id}/read    mark one as read
PATCH /notifications/read-all     mark everything read

Notification CREATION is not exposed as a public endpoint — notifications
are generated server-side (e.g. by app/services/notification_service.py,
triggered when an analysis result changes a crop plan's feasibility
status, or by a scheduled job) rather than something a customer can
fabricate for themselves via the API.
    )	APIRouterDepends)descfuncselectupdate)AsyncSession)get_current_user)AppException)NotificationUser)get_db_session)NotificationListResponseNotificationResponsec                       e Zd ZdZdZy)NotificationNotFoundErrori  NOTIFICATION_NOT_FOUNDN)__name__
__module____qualname__status_code
error_code     OC:\Crop_Prediction\Backend\crop-ai-system\app\api\v1\endpoints\notifications.pyr   r      s    K)Jr   r   z/notifications)response_model2   limitcurrent_userdbreturnc                   K   |j                  t        t              j                  t        j                  |j
                  k(        j                  t        t        j                              j                  t        | d                   d {   }|j                         j                         }|j                  t        t        j                               j                  t              j                  t        j                  |j
                  k(  t        j                   j#                  d                   d {   }|j%                         }t'        |D cg c]h  }t)        |j
                  |j*                  |j,                  |j.                  |j0                  |j                   |j                  j3                               j c}|      S 7 A7 c c}w w)N   Fidtitlemessageseverityrelated_crop_plan_idis_read
created_at)notificationsunread_count)executer   r   whereuser_idr%   order_byr   r+   r   minscalarsallr   countselect_fromr*   is_
scalar_oner   r   r&   r'   r(   r)   	isoformat)r   r   r    resultr,   unread_resultr-   ns           r   list_notificationsr=   !   s]     ::|	|##|6	7	$|../	0	s5#		 F NN$((*M**tzz|	\	"	|##|68L8L8P8PQV8W	X M
 !++-L# #
  !44gg		%&%;%;		<<113
 " 
s8   BGGB,G:G;GA-G
GGGz%/notifications/{notification_id}/readnotification_idc           
      R  K   |j                  t        t              j                  t        j                  | k(  t        j
                  |j                  k(               d {   }|j                         }|t        d      d|_        |j                          d {    |j                  |       d {    t        |j                  |j                  |j                  |j                  |j                  |j                  |j                   j#                               S 7 7 7 nw)NzNotification not found)r'   Tr$   )r.   r   r   r/   r%   r0   scalar_one_or_noner   r*   commitrefreshr   r&   r'   r(   r)   r+   r9   )r>   r   r    r:   notifications        r   mark_notification_readrD   G   s      ::|""OO.0D0D0W	
 F
 ,,.L'0HIIL
))+
**\
"""??  $$&&)>>$$**446  "s7   AD' D!!<D'D#D'6D%7A+D'#D'%D'z/notifications/read-all   )r   Nc                 @  K   |j                  t        t              j                  t        j                  | j
                  k(  t        j                  j                  d            j                  d             d {    |j                          d {    y 7 7 w)NFT)r*   )
r.   r   r   r/   r0   r%   r*   r7   valuesrA   )r   r    s     r   mark_all_notifications_readrH   e   s{     
 **|	|##|68L8L8P8PQV8W	X		  
 ))+
 s$   A:B<B=BBBB) __doc__fastapir   r   
sqlalchemyr   r   r   r   sqlalchemy.ext.asyncior	   app.core.current_userr
   app.core.exceptionsr   app.db.modelsr   r   app.db.sessionr   app.schemas.crop_planr   r   routerr   getintr=   patchstrrD   rH   r   r   r   <module>rW      s?   ' 1 1 / 2 , , ) P	* *
 -EF !12~.""" 	" 	" G"J 5FZ[ !!12~. 	 	 \: 'S9 !12~.			 
	 :	r   