Ë
    KµCjô  ã            
       óÈ  — 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_modelé2   Ú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ß	‰Œs�5˜#‹Ó	ó	÷ €Fð —N‘NÓ$×(Ñ(Ó*€MàŸ*™*ÜŒt�z‰z‹|Óß	‰”\Ó	"ß	‰Œ|×#Ñ# |§¡Ñ6¼×8LÑ8L×8PÑ8PÐQVÓ8WÓ	Xó÷ €Mð
 !×+Ñ+Ó-€Lä#ð #ö
ð ô !Ø—4‘4Ø—g‘gØŸ	™	ØŸ™Ø%&×%;Ñ%;ØŸ	™	ØŸ<™<×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ø   è ø€ ð —:‘:ÜŒ|Ó×"Ñ"Ü�O‰O˜Ñ.´×0DÑ0DÈÏÉÑ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¼×8LÑ8L×8PÑ8PÐQVÓ8WÓ	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 ‡�Ð5ÐFZ€Ó[ñ !Ð!1Ó2Ù˜~Ó.ñØðàðð 	ðð ò	ó \ðð: ‡�Ð'°S€Ó9á Ð!1Ó2Ù˜~Ó.ñ	Øð	àð	ð 
ò	ó :ñ	r   