o
    W/i4                     @  s   d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	m
Z
mZ d dlmZ ee jZed Zed ee e	e
eeedd	gd
Zd!ddZd"ddZd#ddZd$d%ddZd&dd ZdS )'    )annotations)Path)ListDictAny)EnvironmentFileSystemLoaderselect_autoescape)get_mail_theme	templatesFOLDERhtmlxml)loader
autoescapenoder   returnstrc                 C  s*   t | tr| dp| dpdS | pdS )Nverbatimcomputed )
isinstancedictget)r    r   A/var/www/html/flask_server/apps/wp_invoices_mail_app/renderers.py_pick   s   
r   r   c                  C  s\   t  } t| d | d | d | d | d | d | d | d | d	 | d
 | d | d | d dS )N
font_stackpage_bgcard_bgborder_colorbrand_color
text_colorsecondary_textsuccess_colorerror_colorstat_bgtable_header_bgtag_bgtag_text)r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   )r
   r   )themer   r   r   _theme_context   s    r+   resultsList[Dict[str, Any]]c              
   C  s  t  }td}t| }tdd | D }|| }| sd}n|dkr%d}nd| d}g }t| dd	D ]\}}	|	d
pB|	dpBd}
|	dd}|	d}|rR|r]d}d|pXd g}n|	dpci }|di pki }|di psi }|di p{i }|di pi }t|d}t|d}t|dpi d}t|d}|dpd}g }|r|d| d  |r|d!|  |r|d"|  |r|d#| d$|  |s|d% d&}|||
||d' q3d(}|j	d*i ||t
| ||||d)S )+Nzv1/email_results.htmlc                 s  s*    | ]}| d dr| dsdV  qdS )okTerror   N)r   ).0rr   r   r   	<genexpr>5   s   ( z,render_results_email_html.<locals>.<genexpr>zWe received your email but could not process any valid invoice.<br/>Please make sure the attachments are clear invoice images or PDF files.r0   z9We processed the attached invoice and here is the result:zWe processed <strong>z5</strong> attached invoices. Here is a quick summary:)startfilenameoriginal_filenamez(no filename)r.   Tr/   zNot processedzReason: zUnknown error.	extractedheadersupplierinvoicetotalsnamenumberdate
issue_dategrand_totalcurrencyAUDzSupplier: <strong>z	</strong>zInvoice #: zDate: zTotal:  zExtracted successfully.	Processed)indexr5   statusdetailsu"  If something looks wrong, please double-check the original supplier invoice. The system may have doubts in low-quality or partially readable areas.<br/><br/>Review PDFs are attached for every invoice processed successfully. Invoices marked as “Not processed” will not have a review PDF.)
intro_html
show_statsn_okn_errresults_viewfooter_noter   )r+   envget_templatelensum	enumerater   r   appendrenderbool)r,   	theme_ctxtemplatetotalrJ   rK   rH   rL   idxr2   fnameok_flag	error_msgstatus_labelrG   r7   r8   r9   invoice_blockr;   supplier_nameinvoice_numberr?   r@   rA   rM   r   r   r   render_results_email_html0   sz   





ra   Nauth_msg
str | Nonec              	   C  s^   t  }td}d}d}d}g d}| r|d|  d d}|jd
i ||||||d	S )Nv1/email_message.htmlu&   Invoice Bot – Authentication problemz
Auth errorzkWe could not process your email because there was a problem with the login credentials in the message body.)zVPlease make sure you include a valid login in the body of the email using this format:z8<code>USER: your_username<br/>PASS: your_password</code>zdIf the credentials are incorrect, the bot will ignore the message and no invoices will be processed.z<small>Technical detail: z</small>zOnce your login is correct, send the invoice again as an attachment (PDF or clear image) and you will receive a processing summary email.subject	tag_labelrH   
paragraphsrM   r   r+   rN   rO   rS   rT   )rb   rV   rW   rf   rg   rH   rh   rM   r   r   r   render_invalid_credentials_html   s,   


rj   inline_photosrU   c              	   C  sh   t  }td}d}d}d}g }| r|d n|d |d d}|jd
i ||||||d	S )Nrd   u*   Invoice Bot – No valid attachments foundzNo attachmentszHWe received your email but could not find any valid invoice attachments.zIt looks like you inserted a photo inside the email body. To process it, please attach the invoice as a file using the paper-clip icon instead of pasting it into the message text.zmPlease attach at least one invoice as a PDF or a clear image file (JPG, PNG, etc.) so the bot can process it.zIThis message has not been processed and no review PDF has been generated.zAs soon as you send an email with valid attachments, the bot will reply with a processing summary and a review PDF per invoice.re   r   ri   )rk   rV   rW   rf   rg   rH   rh   rM   r   r   r   render_no_attachments_html   s8   

rl   )r   r   r   r   )r   r   )r,   r-   r   r   )N)rb   rc   r   r   )rk   rU   r   r   )
__future__r   pathlibr   typingr   r   r   jinja2r   r   r	   shared.branding.brandingr
   __file__resolveparentBASE_DIRTEMPLATES_ROOTprintr   rN   r   r+   ra   rj   rl   r   r   r   r   <module>   s$   




W)