o
    Ҽh                     @   s   d Z ddlZddlmZ ddlmZmZ ddl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mZmZmZ eeZG dd dZdS )za
Google OAuth service for business logic.
Handles Google OAuth authentication and user creation.
    N)	timedelta)HTTPExceptionstatus)Session)get_password_hash)settings)User)TokenResponseUserResponse)verify_google_tokenget_google_user_infocreate_google_oauth_urlexchange_code_for_tokensc                   @   sh   e Zd ZdZedefddZededefddZ	ededefdd	Z
ed
ededefddZdS )GoogleAuthServicez$Service for Google OAuth operations.returnc                   C   s   t js
ttjddt S )z#Get Google OAuth authorization URL.zGoogle OAuth not configuredstatus_codedetail)r   GOOGLE_CLIENT_IDr   r   HTTP_500_INTERNAL_SERVER_ERRORr    r   r   services/google_auth_service.pyget_oauth_url   s   zGoogleAuthService.get_oauth_urldbc           	   
   C   s   t d zPt| j}|sttjdd|d}|d}|r%t|}n|r,t	|}nttjdd|s<ttjddt
||}|||}t|tjd t|d	W S  ty]     tyw } zt d
|  ttjddd}~ww )z5Handle Google OAuth callback with authorization code.u#   🔄 Google OAuth callback receivedz%Failed to exchange authorization coder   access_tokenid_tokenz#No valid token received from GooglezFailed to verify Google user<   r   
expires_inuseru!   ❌ Google OAuth callback error: z"Google OAuth authentication failedN)loggerinfor   coder   r   HTTP_400_BAD_REQUESTgetr   r   r   _get_or_create_usercreate_user_sessionr	   r   ACCESS_TOKEN_EXPIRE_MINUTESr
   from_orm	Exceptionerrorr   )		auth_datar   auth_servicetoken_responser   r   google_userr   er   r   r   handle_oauth_callback(   sP   





z'GoogleAuthService.handle_oauth_callbackc              
   C   s   t d z(t| j}|sttjddt||}|	||}t
|tjd t|dW S  ty5     tyO } zt d|  ttjddd}~ww )	z'Authenticate user with Google ID token.u)   🔄 Google token authentication receivedzInvalid Google tokenr   r   r   u'   ❌ Google token authentication error: zGoogle authentication failedN)r    r!   r   r   r   r   HTTP_401_UNAUTHORIZEDr   r%   r&   r	   r   r'   r
   r(   r)   r*   r   )r+   r   r,   r.   r   r   r/   r   r   r   authenticate_with_tokena   s2   

z)GoogleAuthService.authenticate_with_tokenr.   c              
   C   s   | ttj| d k }|sbt| d | ddtd| d  ddd}z|| |  |	| t
d	| d   W |S  tya } zt
d
|  |  ttjddd}~ww t
d| d   |S )z5Get existing user or create new one from Google data.emailname google_subpartnerfree)r3   r4   password	user_typerateu'   ✅ New user created via Google OAuth: u*   ❌ Error creating user via Google OAuth: zError creating userr   Nu.   ✅ Existing user logged in via Google OAuth: )queryr   filterr3   firstr$   r   addcommitrefreshr    r!   r)   r*   rollbackr   r   r   )r.   r   r   r/   r   r   r   r%      s4   


z%GoogleAuthService._get_or_create_userN)__name__
__module____qualname____doc__staticmethodstrr   r   r	   r0   r2   dictr   r%   r   r   r   r   r      s    
8"r   )rG   loggingdatetimer   fastapir   r   sqlalchemy.ormr   
auth_utilsr   configr   modelsr   schemasr	   r
   google_oauthr   r   r   r   	getLoggerrD   r    r   r   r   r   r   <module>   s    
