o
    ag                     @   s   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Zd dlmZ d dlmZ d dlmZ d d	lmZ d dlZed
ZG dd dZdS )    )ListOptionalDictAny)Session)Notification)NotificationResponseN)datetime)ZoneInfo)HTTPException)text	adimsayarc                   @   s   e Zd Zedededee fddZedededede	e
 fddZeddeded
edededeeef de
fddZeddedededed
edeeef defddZededefddZedededede	e
 fddZd	S )NotificationServicedbuser_idreturnc                 C   sl   |  ttj|ktd }tdt| d|  |D ]}t	d|j
 d|j  qdd |D S )u_   
        사용자의 알림을 조회합니다 (소프트 삭제된 알림은 제외)
        zf(data IS NULL OR JSON_EXTRACT(data, '$.deleted') IS NULL OR JSON_EXTRACT(data, '$.deleted') = 'false')zget_notifications: found z notifications for user_id zNotification id: z, data: c                 S   s   g | ]}t |qS  )r   from_orm).0notificationr   r   )/app/app/services/notification_service.py
<listcomp>!   s    z9NotificationService.get_notifications.<locals>.<listcomp>)queryr   filterr   r   allloggerinfolendebugiddata)r   r   notificationsnr   r   r   get_notifications   s   
z%NotificationService.get_notificationsnotification_idc                 C   s   |  ttj|ktj|k }|d u r"td| d|  d S t	t
d|_|   | | td| d|  |S )NNotification with id ) not found or not authorized for user_id zEurope/IstanbulzNotification id z marked as read for user_id )r   r   r   r   r   firstr   warningr	   nowr
   read_atcommitrefreshr   )r   r$   r   r   r   r   r   mark_notification_as_read#   s   

z-NotificationService.mark_notification_as_readN
notif_typetitlemessager    c                 C   s    t |||||d}| | |S )uZ  
        알림을 생성하고 데이터베이스에 추가합니다. 
        주의: 이 메서드는 db.commit()을 호출하지 않으므로, 호출하는 쪽에서 처리해야 합니다.
        
        푸시 알림 기능을 위해 호출하는 쪽에서 선택적으로 커밋을 제어할 수 있도록 설계되었습니다.
        r   typer/   r0   r    )r   add)r   r   r.   r/   r0   r    new_notificationr   r   r   create_notification3   s   
z'NotificationService.create_notificationgeneralT	send_pushc           	   	   C   sr   t |||||d}| | |   | | td| d|j  |r7ddlm} |j	| |||||dd |S )u~   
        알림을 생성하고 데이터베이스에 저장하며, 선택적으로 푸시 알림을 발송합니다.
        r1   z/New notification saved to database for user_id z	 with ID r   )ExpoPushNotificationServiceF)r   r   r/   r0   r    notification_typeZ
save_to_db)
r   r3   r+   r,   r   r   r   Z+app.services.expo_push_notification_servicer8   Zsend_push_to_user)	r   r   r/   r0   r.   r    r7   r4   r8   r   r   r   send_notificationE   s.   


z%NotificationService.send_notificationc                 C   s   t d tj| |ddddS )u/   사용자 로그인 시 환영 메시지 전송u)   New Client Login – Welcome Message Sentu   Hoş geldiniz!u;   Adimsayar'a hoş geldiniz. İyi vakit geçirmenizi dileriz!u   hoş_geliş)r   r   r/   r0   r.   )r   r   r   r:   )r   r   r   r   r   send_welcome_notificationg   s   
z-NotificationService.send_welcome_notificationc           
   
   C   s<  t d| d|  | ttj|ktj|k }|du r-t d| d|  dS |j	p1i }t d|  t
  }d|d< ||d	< t|}z3| td
|||d}|   t d|j d | ttj|k }t d|j	  |W S  ty }	 z|   t jdt|	 dd  d}	~	ww )u]   
        알림을 소프트 삭제합니다 (MySQL JSON 필드 직접 업데이트)
        z'Attempting to soft delete notification z
 for user Nr%   r&   zCurrent data before update: Tdeleted
deleted_atz
                    UPDATE notifications 
                    SET data = :json_data 
                    WHERE id = :notification_id AND user_id = :user_id
                )	json_datar$   r   zUpdate result: z rows affectedzUpdated notification data: z"Error updating notification data: )exc_info)r   r   r   r   r   r   r   r'   r(   r    r	   r)   	isoformatjsondumpsexecuter   r+   rowcount	Exceptionrollbackerrorstr)
r   r$   r   r   Zcurrent_datar=   r>   resultZupdated_notificationer   r   r   soft_delete_notifications   sN   



z,NotificationService.soft_delete_notification)N)r6   NT)__name__
__module____qualname__staticmethodr   intr   r   r#   r   r   r-   rH   r   r   r5   boolr:   r;   rK   r   r   r   r   r      s     22!$r   )typingr   r   r   r   sqlalchemy.ormr   Zapp.models.notificationr   app.schemas.notificationr   loggingr	   zoneinfor
   fastapir   
sqlalchemyr   rA   	getLoggerr   r   r   r   r   r   <module>   s   
