
    Dj^                     *   d 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
mZ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 dd
lmZ ddlmZ ddlmZmZmZmZm Z   e       Z! ee"      Z# G d de      Z$e!jK                  ded       ee      fde dedefd       Z&e!jK                  de       ee      fdededefd       Z'e!jK                  de       ee      fdededefd       Z(e!jS                  de       ee      fdedefd       Z*y)u  
Customer authentication endpoints for the SaaS web application.

POST /auth/register  — create a new customer account
POST /auth/login      — exchange email+password for access+refresh tokens
POST /auth/refresh    — exchange a refresh token for a new access token
GET  /auth/me         — return the logged-in customer's profile

This is intentionally a self-contained, minimal auth flow (no OAuth/SSO,
no email verification flow implemented yet — is_verified exists as a
field for a future email-confirmation feature but isn't enforced at
login). That scope was confirmed with the project coordinator as
sufficient for initial SaaS launch; see docs/architecture.md.
    )datetime)	APIRouterDepends)select)AsyncSession)ACCESS_TOKEN_EXPIRE_MINUTEScreate_access_tokencreate_refresh_tokendecode_tokenhash_passwordverify_password)get_current_user)AppExceptionAuthenticationError)
get_logger)User)get_db_session)RefreshTokenRequestTokenResponseUserLoginRequestUserProfileResponseUserRegisterRequestc                       e Zd ZdZdZy)EmailAlreadyRegisteredErrori  EMAIL_ALREADY_REGISTEREDN)__name__
__module____qualname__status_code
error_code     FC:\Crop_Prediction\Backend\crop-ai-system\app\api\v1\endpoints\auth.pyr   r   /   s    K+Jr"   r   z/auth/register   )response_modelr   payloaddbreturnc                   K   |j                  t        t              j                  t        j                  | j                  j                         k(               d {   }|j                         t        d      t        | j                  j                         t        | j                        | j                  | j                        }|j                  |       |j                          d {    |j                          d {    t        j!                  d|j"                          t%        t'        |j"                  |j                        t)        |j"                        t*              S 7 7 7 nw)NzBAn account with this email already exists. Try logging in instead.message)emailhashed_password	full_nameorganizationzNew user registered: user_id=access_tokenrefresh_tokenexpires_in_minutes)executer   r   wherer,   lowerscalar_one_or_noner   r   passwordr.   r/   addflushcommitloggerinfoidr   r	   r
   r   )r&   r'   existingusers       r#   registerrA   4   s    
 ZZt 2 24::ATATAV3V WXXH""$0)X
 	
 mm!!#%g&6&67##))	D FF4L
((*
))+
KK/y9:($**=*47736 % Y s8   AE8E1BE8/E40E8E6A*E84E86E8z/auth/login)r%   c                 b  K   |j                  t        t              j                  t        j                  | j                  j                         k(               d {   }|j                         }| t        | j                  |j                        st        d      |j                  st        d      t        j                         |_        |j                          d {    t!        t#        |j$                  |j                        t'        |j$                        t(              S 7 7 Iw)NzIncorrect email or passwordr*   z3This account has been deactivated. Contact support.r0   )r4   r   r   r5   r,   r6   r7   r   r8   r-   r   	is_activer   utcnowlast_login_atr;   r   r	   r>   r
   r   )r&   r'   resultr@   s       r#   loginrG   R   s     
 ::fTl00w}}?R?R?T1TUVVF$$&D |?7+;+;T=Q=QR!*GHH>>!*_`` "*D
))+($**=*47736  W s&   AD/D+BD/#D-$AD/-D/z/auth/refreshc                   K   t        | j                  d      }|j                  d      }|j                  t	        t
              j                  t
        j                  |k(               d {   }|j                         }||j                  st        d      t        t        |j                  |j                        t        |j                        t              S 7 qw)Nrefresh)expected_typesubz-Account no longer valid. Please log in again.r*   r0   )r   r2   getr4   r   r   r5   r>   r7   rC   r   r   r	   r,   r
   r   )r&   r'   decodeduser_idrF   r@   s         r#   r2   r2   m   s     
 700	JGkk% G::fTl00G1CDEEF$$&D|4>>!*YZZ($**=*47736  Fs   A*C ,C-A2C z/auth/mecurrent_userc           	         K   t        | j                  | j                  | j                  | j                  | j
                  | j                  j                               S w)N)r>   r,   r.   r/   is_verified
created_at)r   r>   r,   r.   r/   rQ   rR   	isoformat)rO   s    r#   get_profilerT      sP     ??  ((!.. ,,**446 s   AAN)+__doc__r   fastapir   r   
sqlalchemyr   sqlalchemy.ext.asyncior   app.core.auth_tokensr   r	   r
   r   r   r   app.core.current_userr   app.core.exceptionsr   r   app.core.logging_configr   app.db.modelsr   app.db.sessionr   app.schemas.authr   r   r   r   r   routerr   r<   r   postrA   rG   r2   rL   rT   r!   r"   r#   <module>rb      si    &  /  3 A .  )  
	H	,, ,
 mM ~.   N: ]=9 ~.  :4 _]; ~.   <( J':;+23C+D D I\  <r"   