o
    h                     @   s   d Z ddl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mZ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 )zf
Authentication service for business logic.
Handles user registration, login, and session management.
    N)	timedelta)Optional)HTTPExceptionstatus)Session)create_access_tokenget_password_hashget_token_expirationverify_password)settings)UserUserSession)	UserLoginUserRegisterTokenResponseUserResponsec                   @   s   e Zd ZdZedededefddZed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edededefddZdS )AuthServicez&Service for authentication operations.	user_datadbreturnc              
   C   s   t d| j  |ttj| jk r't d| j  tt	j
ddt| j| jt| jddd}z|| |  || t d| j d	|j d
 W n# tyv } zt d| j d|  |  tt	jddd}~ww dddS )zRegister new user.u   📝 Registration attempt: u.   ❌ Registration attempt with existing email: z#User with this email already existsstatus_codedetailpartnerfree)emailnamepassword	user_typerateu"   ✅ User registered successfully:  (ID: )u   ❌ Registration error for : zError creating userNzUser registered successfullyTmessagesuccess)loggerinfor   queryr   filterfirstwarningr   r   HTTP_400_BAD_REQUESTr   r   r   addcommitrefreshid	ExceptionerrorrollbackHTTP_500_INTERNAL_SERVER_ERROR)r   r   new_usere r7   services/auth_service.pyregister_user   s:   

 
zAuthService.register_userc              
   C   sD  t d| j  |ttj| jk }|s)t d| j  tt	j
ddt| j|js@t d| j  tt	j
ddtdt|jittjdd}t|j|t d	}z|| |  t d
| j d|j d W n# ty } zt d| j d|  |  tt	jddd}~ww t|tjd t|dS )z%Authenticate user and create session.u   🔐 Login attempt: u+   ❌ Login attempt with non-existent email: zInvalid email or passwordr   u   ❌ Invalid password for user: subminutesdataexpires_deltauser_id
token_hash
expires_atu   ✅ Login successful: r    r!   u   ❌ Session creation error for r"   Error creating sessionN<   )access_token
expires_inuser)r&   r'   r   r(   r   r)   r*   r+   r   r   HTTP_401_UNAUTHORIZEDr
   r   r   strr0   r   r   ACCESS_TOKEN_EXPIRE_MINUTESr   r	   r-   r.   r1   r2   r3   r4   r   r   from_orm)r   r   rH   rF   sessionr6   r7   r7   r8   
login_userB   sR   

 zAuthService.login_usercurrent_userc                 C   s&   t d| j d| j d dddS )zLogout user.u   🚪 Logout: r    r!   zLogout successfulTr#   )r&   r'   r   r0   )rO   r7   r7   r8   logout_userw   s   
zAuthService.logout_userc                 C   s:   t | j|jsttjddt| j|_|  dddS )zChange user password.zInvalid current passwordr   zPassword changed successfullyTr#   )	r
   current_passwordr   r   r   r,   r   new_passwordr.   )password_datarO   r   r7   r7   r8   change_password~   s   
zAuthService.change_passwordrH   c              
   C   s   t dt| jittjdd}t| j|t d}z|| |	  W |S  t
yG } ztd| j d|  |  ttjddd	}~ww )
z,Create user session and return access token.r:   r;   r=   r@   u$   ❌ Session creation error for user r"   rD   r   N)r   rJ   r0   r   r   rK   r   r	   r-   r.   r1   r&   r2   r   r3   r   r   r4   )rH   r   rF   rM   r6   r7   r7   r8   create_user_session   s,   


	zAuthService.create_user_sessionN)__name__
__module____qualname____doc__staticmethodr   r   dictr9   r   r   rN   r   rP   rT   rJ   rU   r7   r7   r7   r8   r      s$    $4r   )rY   loggingdatetimer   typingr   fastapir   r   sqlalchemy.ormr   
auth_utilsr   r   r	   r
   configr   modelsr   r   schemasr   r   r   r   	getLoggerrV   r&   r   r7   r7   r7   r8   <module>   s    
