o
    sg                     @   s  d dl Z d dlZd dlZd dlmZ d dlZd dlmZmZ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 e ZeddgdZe Zejdedde fddZ!ej"dedde defddZ#e$dde defddZ%e$dde de&fddZ'e(dedfde d efd!d"Z)e*ded#d$d%fde d&e&fd'd(Z+dS ))    N)datetime)	APIRouterBodyFileHeaderHTTPExceptionQuery
UploadFilestatus)JSONResponse)AccountTypeUserBase)validate_jwt_token)UserService)S3BucketStorage   )	utilitiesz/usersUsers)prefixtagsz
/{user_id})response_modeluser_idc                    s*   t | }|sttjd|  dd|S )z&Get user details for the given user_idUser with ID 
 not foundstatus_codedetail)user_serviceget_user_by_idr   r
   HTTP_404_NOT_FOUND)r   	user_data r!   3/var/www/html/XCapMarket/controllers/user_router.pyget_user   s   

r#   r    c              
      s   | |j krttjddtjtjj| }|s#ttjd|  ddz%|	  t
d|j  d tjtjj|  t
d|j   |W S  ty` } zttjdt| dd	}~ww )
z)Update user details for the given user_idz6User ID in path does not match user ID in request bodyr   r   r   zUpdating user z in DynamoDBzSuccessfully updated user zFailed to update user: N)r   r   r
   HTTP_400_BAD_REQUESTr   dbget_itemuserr   update_modify
XCM_loggerinfoupload_to_dynamodb
model_dump	ExceptionHTTP_500_INTERNAL_SERVER_ERRORstr)r   r    existing_userer!   r!   r"   update_user(   s2   

r2   z/{user_id}/account-typeaccount_typec              
      s   t jt jj| }|sttjd|  ddztdi |}|| t j	t jj|
  ddiW S  tyK } zttjdt| dd}~ww )	zUpdate user's account typer   r   r   messagez!Account type updated successfullyzFailed to update account type: Nr!   )r   r%   r&   r'   r   r
   r   r   update_account_typer+   r,   r-   r.   r/   )r   r3   r    r'   r1   r!   r!   r"   r5   I   s&   


r5   z/{user_id}/status	is_activec              
      s   t jt jj| }|sttjd|  ddz*tdi |}|r&|  n|	  t j
t jj|  dd|r;dnd diW S  tyY } zttjd	t| dd
}~ww )z#Activate or deactivate user accountr   r   r   r4   zUser 	activateddeactivatedz successfullyzFailed to update user status: Nr!   )r   r%   r&   r'   r   r
   r   r   activate
deactivater+   r,   r-   r.   r/   )r   r6   r    r'   r1   r!   r!   r"   update_user_status_   s,   

r;   z/{user_id}/profile-picture.filec              
      s\  t jt jj| }|sttjd|  ddz~|jds%ttj	dd|j
dd  }|dvr:ttj	d	dd
|  dt d d| }tjd }t|d}| I dH }|j||d |j}td|   tdi |}	|	j|d t jt jj|	  tdd|t|dddW S  t y }
 zttj!dt"|
 dd}
~
ww )zUpload user profile picturer   r   r   zimage/zFile must be an image.)jpgjpegpnggifsvgwebpz9Only jpg, jpeg, png, webp, svg, and gif files are allowedzprofile-pictures//z%Y%m%d_%H%M%Sxcap_s3_storage)bucket_nameN)s3_file_paths3_filez/Successfully uploaded profile picture for user )profile_pic   z%Profile picture uploaded successfullyzutf-8)r4   profile_pic_url
image_blobr   contentz"Failed to upload profile picture: r!   )#r   r%   r&   r'   r   r
   r   content_type
startswithr$   filenamesplitlowerr   nowstrftimeosenvironr   readupload_file_to_s3_buckets3_file_locationr)   r*   r   update_profiler+   r,   r   base64	b64encodedecoder-   r.   r/   )r   r<   r    file_extrH   rF   
s3_storagefile_contents3_urlr'   r1   r!   r!   r"   upload_profile_picturez   s\   
 


rd   Fz1Whether to deactivate the user in Cognito as well)descriptiondeactivate_cognitoc              
      s   zVt  }|| }|sttjdd|  |  |rLztd}|j	t
jd |jd W n tyK } ztdt|  W Y d}~nd}~ww ttjd| |d	d
W S  tye } z|d}~w ty| } zttjdt| dd}~ww )zDelete/deactivate a userzUser not foundr   zcognito-idpCOGNITO_USER_POOL_ID)
UserPoolIdUsernamez&Failed to deactivate user in Cognito: NzUser successfully deactivated)r4   r   cognito_deactivatedrN   zFailed to delete user: )r   r   r   r
   r   r:   
save_to_dbboto3clientadmin_disable_userrW   rX   emailr-   r)   errorr/   r   HTTP_200_OKr.   )r   rf   r   r'   cognito_clientr1   http_exr!   r!   r"   delete_user   sJ   


 	rt   ),r]   loggingrW   r   rl   fastapir   r   r   r   r   r   r	   r
   fastapi.responsesr   models.userr   r   services.auth_servicer   services.user_servicer   utils.s3_storager    r   	getLoggerr)   r'   r   getr/   r#   putr2   patchr5   boolr;   postrd   deletert   r!   r!   r!   r"   <module>   sD    ( >