o
    g                     @   s   d dl Z d dlZd dlZd dlmZ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 edZG dd	 d	ZdS )
    N)ListDictOptionalAnyUnion)Session)Notification)DeviceTokenService)settings	adimsayarc                   @   s   e Zd ZdZdZe			ddededed	ed
e	ee
f dededefddZe	dde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	ee
f dedefddZdS )ExpoPushNotificationServiceuN   Expo Push 알림 서비스 - FCM과는 별도의 서비스로 동작합니다.z$https://exp.host/--/api/v2/push/sendNgeneralTdbuser_idtitlemessagedatanotification_type
save_to_dbreturnc              
   C   s   zXt | |}|s!td| d |rt| ||||| W dS dd |D }|sBtd| d |r?t| ||||| W dS t||||}	|rVt| ||||| |	W S  tyu }
 ztdt	|
  W Y d}
~
d	S d}
~
ww )
u  
        Expo Push Service를 통해 사용자에게 푸시 알림을 보내고, 선택적으로 DB에 알림을 저장합니다.
        NotificationService에서 호출될 때는 이미 DB에 저장되었을 수 있으므로 save_to_db 파라미터로 제어합니다.
           사용자 ID u<   에 대한 활성화된 디바이스 토큰이 없습니다.Tc                 S   s   g | ]}|j d r|j qS )zExponentPushToken[)device_token
startswith).0token r   3/app/app/services/expo_push_notification_service.py
<listcomp>-   s    
zAExpoPushNotificationService.send_push_to_user.<locals>.<listcomp>u4   에 대한 Expo 디바이스 토큰이 없습니다.u"   Expo 푸시 알림 전송 오류: NF)
r	   get_user_device_tokensloggerinfor   _save_notification_to_db_send_expo_push	Exceptionerrorstr)r   r   r   r   r   r   r   device_tokensZexpo_tokenssuccesser   r   r   send_push_to_user   s<   z-ExpoPushNotificationService.send_push_to_usertokensc              
   C   s  z| st d W dS dddd}g }| D ]}|||dd}|r$||d< || qd	}td	t|d
D ]l}	||	|	d
  }
tjtj||
d}|j	dkr|
 }d|v rt|d D ]7\}}|ddkr{|d7 }t d| |	|  dd  d qZt d| |	|  dd  dt
|  qZq4t d|j	 d|j  q4t d| dt|   |d	kW S  ty } zt dt|  W Y d}~dS d}~ww )uA   Expo Push Service API를 통해 푸시 알림을 전송합니다.u4   푸시 알림을 보낼 Expo 토큰이 없습니다.Fzapplication/jsonzgzip, deflate)zContent-TypeAcceptzAccept-Encodingdefault)tor   bodysoundr   r   d   )headersjson   statusok   u)   Expo 푸시 알림 티켓 발행 성공: N   z...u)   Expo 푸시 알림 티켓 발행 실패: z... - u&   Expo 푸시 알림 API 응답 오류: z - u)   Expo 푸시 알림 전송 결과: 성공 /u-   Expo 푸시 알림 전송 중 오류 발생: )r   warningappendrangelenrequestspostr   EXPO_PUSH_APIstatus_coder2   	enumerategetr    dumpstextr#   r$   r%   )r*   r   r   r   r1   messagesr   Zmessage_objsuccess_countibatchresponseresponse_dataidxZticketr(   r   r   r   r"   J   sV   

$.
z+ExpoPushNotificationService._send_expo_pushc              
   C   s   z't |||||d}| | |   | | td| d|j d |W S  tyB } z|   t	dt
|   d}~ww )uB   
        알림을 데이터베이스에 저장합니다.
        )r   typer   r   r   r   u7   에 대한 알림이 DB에 저장되었습니다. (ID: )u   알림 DB 저장 오류: N)r   addcommitrefreshr   r    idr#   rollbackr$   r%   )r   r   r   r   r   r   notificationr(   r   r   r   r!      s&   
	
z4ExpoPushNotificationService._save_notification_to_db)Nr   T)N)Nr   )__name__
__module____qualname____doc__r?   staticmethodr   intr%   r   r   boolr)   r   r"   r   r!   r   r   r   r   r      sn    
8
B
r   )r2   loggingr=   typingr   r   r   r   r   sqlalchemy.ormr   Zapp.models.notificationr   !app.services.device_token_servicer	   app.core.configr
   	getLoggerr   r   r   r   r   r   <module>   s    
