
    J)i              
          d dl Z d dlmZ d dlZd dlZd dl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Zd dlmZ d dlmZ  e         ej$                  ej&                          ej(                  d	      Z ej(                  d
      Z ej(                  d      Z e ej(                  dd            Z ej(                  d      Z ej(                  dd      j7                         dk(  Z ej(                  dd      j7                         dk(  ZdZdedefdZ d&dedefdZ!dededede"fdZ#dededefdZ$dededefd Z%d!ed"ed#ed$ede&f
d%Z'y)'    N)MIMEText)load_dotenv)FastMailMessageSchema)conf)EmailMessage)settings)levelMAIL_USERNAMEMAIL_PASSWORD	MAIL_FROM	MAIL_PORTK  MAIL_SERVERMAIL_STARTTLSTruetrueMAIL_SSL_TLSFalse   to_emailotpc                 >   d}d| d}t        |      }||d<   t        |d<   | |d<   	 t        j                  dt         dt
         d	       t        j                  t        t
              5 }t        r%|j                          t        j                  d
       |j                  t        t               |j                  |       t        j                  d|         ddd       y# 1 sw Y   yxY w# t        $ r%}t        j                  d|  d|        Y d}~yd}~ww xY w)z)Send OTP code to user's email using SMTP.zPassword Reset OTPzYour OTP code is: z%

This OTP will expire in 10 minutes.SubjectFromTozConnecting to SMTP server :z...zSTARTTLS enabledu#   ✅ OTP email sent successfully to Nu   ❌ Failed to send OTP to z: )r   r   logginginfor   r   smtplibSMTPr   starttlsloginr   r   send_message	Exceptionerror)r   r   subjectbodymsgserveres          6/var/www/html/retail-simulation-api/app/utils/email.pysend_otp_emailr-      s    "Gu$KLD
4.CC	NCKCID1+a	{#NO\\+y1V!/0LL6$LL>xjIJ 211  D28*BqcBCCDs7   AC. *A/C"C. "C+'C. +C. .	D7DDlengthreturnc                     t         j                  t         j                  z   dz   dj                  fdt	        |       D              S )zAGenerate a random password with letters, digits, and punctuation.z
!@#$%^&*() c              3   H   K   | ]  }t        j                          y wN)randomchoice).0_charss     r,   	<genexpr>z+generate_random_password.<locals>.<genexpr>:   s     ?A6=='s   ")stringascii_lettersdigitsjoinrange)r.   r8   s    @r,   generate_random_passwordr?   7   s5      6==0<?E77?v???    tor'   r(   c                 
   	 t               }t        j                  |d<   | |d<   ||d<   |j                  |       t	        j
                  t        j                  t        j                        5 }t        j                  r|j                          |j                  t        j                  t        j                         |j                  |       d d d        y# 1 sw Y   yxY w# t        $ r}t        d|        Y d }~yd }~ww xY w)Nr   r   r   Tz Unexpected error sending email: F)r   r	   r   set_contentr    r!   r   r   r   r"   r#   r   r   r$   r%   print)rA   r'   r(   r)   r*   r+   s         r,   
send_emailrE   u   s    n((FD	 I\\(..0B0BCv%%!LL//1G1GH$ D  D  045s7   A*C! ,A CC! CC! C! !	D*C==Dc                 h   d}d}d}d}t        |d      }||d<   ||d<   | |d<   t        j                  ||      5 }|j                          |j	                          |j                          |j                  ||       |j                  || g|j                                d d d        y # 1 sw Y   y xY w)	Nzsmtp.stackmail.comr   zinfo@compunet.solutions	Jn193518fplainr   r   r   )r   r    r!   ehlor"   r#   sendmail	as_string)	rA   r'   r(   	smtp_host	smtp_port	smtp_user	smtp_passr)   r*   s	            r,   send_email_syncrP      s    (		-		tW% IFD	\\)Y/6KKMOOKKMLLI.OOItS]]_= 0//s   A%B((B1c                 z   K   t        j                         }|j                  d t        | ||       d {    y 7 wr3   )asyncioget_running_looprun_in_executorrP   )rA   r'   r(   loops       r,   send_email_asyncrV      s9     '')""
 	
 	
s   1;9;nameemailpassword	login_urlc           	      ,    d}d|  d| d| d| d	}||fS )NzYour New Account Detailsz
Hello z=,

Your admin account has been created successfully.

Email: z
Password: z

Login using the link below:
zY

Please log in and change your password immediately.

Thank you,
Retail Simulation Team
 )rW   rX   rY   rZ   r'   r(   s         r,   admin_account_createdr]      sK    (Gf  	w *   D" 4<r@   )
   )(rR   email.mime.textr   r   osr4   r:   dotenvr   fastapi_mailr   r   
app.configr   r    email.messager   r	   basicConfigINFOgetenvr   r   r   intr   r   lowerr   r   RESET_OTP_EXPIRE_MINUTESstrr-   r?   boolrE   rP   rV   tupler]   r\   r@   r,   <module>rn      s    $  	    0   &     ',, ' 		/*		/*BIIk"				+s+,	bii&		/6288:fDryy1779VC DS Ds D0@S @# @|3  C D *> >c > >$
s 
S 
 
 C 3 3 SX r@   