
    @vai2                     r   d Z ddlmZmZmZmZ ddlmZ  eded          Z	e	
                    ddg	          d
             Ze	
                    ddg	          ed                         Ze	
                    ddg	          d             Ze	
                    ddg	          ed                         ZdS )a   
app/routes/auth.py
==================
Routes autenticazione.

Endpoint:
- POST /api/v1/auth/login         - Login e ottieni token
- POST /api/v1/auth/logout        - Logout (lato client rimuove token)
- POST /api/v1/auth/refresh-token - Refresh JWT token
    )	Blueprintrequestjsonifyg)require_authauthz/api/v1/auth)
url_prefixz/loginPOST)methodsc                     	 t          j                    } | st          ddd          dfS |                     dd                                          }|                     dd          }|                     dd	          }|r|st          dd
d          dfS t          j        d          }|st          ddd          dfS |                    |||          \  }}}|rt          d|d          dfS t          d|||j        dz  d          dfS # t          $ r.}t          ddt          |           d          dfcY d}~S d}~ww xY w)aW  
    POST /api/v1/auth/login
    
    Login user e ottieni JWT token.
    
    Request body:
    {
        "email": "operatore@studium.it",
        "password": "password123",
        "tenant_id": 1  (opzionale, default 1)
    }
    
    Response success (200):
    {
        "success": true,
        "token": "eyJhbGciOiJIUzI1NiIs...",
        "user": {
            "id": 1,
            "email": "operatore@studium.it",
            "nome": "Operatore Demo",
            "ruolo": "operatore",
            "id_tenant": 1
        },
        "expires_in": 86400
    }
    
    Response error (401):
    {
        "success": false,
        "error": "Invalid password"
    }
    
    Esempio curl:
    curl -X POST http://localhost:5000/api/v1/auth/login       -H "Content-Type: application/json"       -d '{"email":"demo@studium.it","password":"demo123"}'
    FzRequest body is emptysuccesserror  email password	tenant_id   zEmail and password are requiredauth_serviceAuth service not initialized    T  )r   tokenuser
expires_in   zLogin error: N)
r   get_jsonr   getstripr   loginjwt_expiration_hours	Exceptionstr)	datar   r   r   r   r   	user_datar   es	            </var/www/enigma.pooltech.it/enigma_inventario/routes_auth.pyr"   r"      s   N3!! 	 0     
 "%%++--88J++HH[!,,	  	H 	 :      u^,, 	 7      #/"4"4UHi"P"Py% 	       &;dB	
 
  
  	    -SVV--
 
    	 	 	 	 	 	s5   (D A+D )D 0D 2D 
E#E=EEz/logoutc                     	 t          dd|| d          dfS # t          $ r.}t          ddt          |           d          dfcY d	}~S d	}~ww xY w)
u  
    POST /api/v1/auth/logout
    
    Logout user. NOTA: il token viene rimosso dal client.
    
    Questo endpoint è più che altro una formalità per log/audit.
    In realtà il logout succede quando client elimina il token
    da localStorage.
    
    Headers richiesti:
    Authorization: Bearer <token>
    
    Response:
    {
        "success": true,
        "message": "Logged out successfully"
    }
    
    Esempio curl:
    curl -X POST http://localhost:5000/api/v1/auth/logout       -H "Authorization: Bearer <token>"
    TzLogged out successfully)r   messageuser_idr   r   FzLogout error: r   r   N)r   r$   r%   )r   r,   kwargsr(   s       r)   logoutr.   p   s    2 0"	
 
  
  	    .c!ff..
 
    	 	 	 	 	 	s    
A#AAAz/refresh-tokenc                     	 t          j                    } | rd| vrt          ddd          dfS |                     d          }t	          j        d          }|st          ddd          dfS |                    |          \  }}|rt          d|d          d	fS t          d
||j        dz  d          dfS # t          $ r.}t          ddt          |           d          dfcY d}~S d}~ww xY w)u	  
    POST /api/v1/auth/refresh-token
    
    Refresh JWT token (ottieni nuovo token senza re-autenticarsi).
    
    Utile quando:
    - Token sta per scadere
    - Client vuole estendere sessione
    - Token è scaduto ma vogliamo dare una chance al client
    
    Request body:
    {
        "token": "eyJhbGciOiJIUzI1NiIs..."
    }
    
    Response success (200):
    {
        "success": true,
        "token": "eyJhbGciOiJIUzI1NiIs...",
        "expires_in": 86400
    }
    
    Response error (401):
    {
        "success": false,
        "error": "Cannot refresh invalid token"
    }
    
    Esempio curl:
    curl -X POST http://localhost:5000/api/v1/auth/refresh-token       -H "Content-Type: application/json"       -d '{"token":"eyJhbGciOiJIUzI1NiIs..."}'
    r   FzToken is requiredr   r   r   r   r   r   Tr   )r   r   r   r   zToken refresh error: N)	r   r   r   r    r   refresh_tokenr#   r$   r%   )r&   	old_tokenr   	new_tokenr   r(   s         r)   r0   r0      s   F$!! 	wd** ,     
 HHW%%	u^,, 	 7     
 (55i@@	5 	      
 &;dB
 
   	 	    5SVV55
 
    	 	 	 	 	 	s.   ,B9 >B9 .-B9 B9 9
C1#C,&C1,C1z/meGETc                 2    t          d|| |dd          dfS )a  
    GET /api/v1/auth/me
    
    Get info del user attualmente loggato.
    
    Richiede autenticazione.
    
    Response:
    {
        "success": true,
        "user": {
            "user_id": 1,
            "email": "operatore@studium.it",
            "tenant_id": 1,
            "ruolo": "operatore"
        }
    }
    
    Esempio curl:
    curl -X GET http://localhost:5000/api/v1/auth/me       -H "Authorization: Bearer <token>"
    T)r,   r   ruolo)r   r   r   )r   )r   r,   r5   r-   s       r)   get_current_userr6      s@    2 "
 
    	     N)__doc__flaskr   r   r   r   middleware_authr   __name__auth_bprouter"   r.   r0   r6    r7   r)   <module>r?      s?  	 	 1 0 0 0 0 0 0 0 0 0 0 0 ( ( ( ( ( ( )FH
@
@
@ 	x&**Y Y +*Yx 	y6(++& &  ,+&R 	&22F F 32FR 	uug&&   '&  r7   