
    Kh*                         d Z ddlmZmZ 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
ej                            d e ee          j        j                             ddlmZmZmZmZ ddlmZ  G d d          ZdS )	zV
Website Service
Handles website generation and management using DeepSite AI workflow
    )DictAnyN)Path)parse_json_inputformat_website_promptsetup_template_copy_logo_files)	AIServicec                       e Zd Zd Zdeeef         deeef         fdZdeeef         defdZde	dedefd	Z
deeef         fd
ZdedefdZde	deddfdZde	de	ddfdZdS )WebsiteServicec                     t          d          | _        | j                            dd           t                      | _        d S )Nztemplates/generatedT)parentsexist_ok)r   generated_pathmkdirr
   
ai_service)selfs    G/var/www/html/web-builder-api.evdpl.com/src/services/website_service.py__init__zWebsiteService.__init__   s>    "#899!!$!>>>#++    datareturnc                 z  K   	 t          |          }t          |          }t          |           d{V }|d         }t          |d                   }t	          ||           d{V }| j                            |d|            d{V }|                    d          r	|d         }	n|                     |          }	| 	                    |	          }	|dz  }
| 
                    |
|	           d{V  |	|t          |          |dS # t          $ r$}t          d	t          |                     d}~ww xY w)
zG
        Generate website from data using DeepSite AI workflow
        Nsafe_folder_namedestination_pathwebsite_promptconversation_idcontent
index.html)html_contentfolder_namewebsite_path
logo_fileszWebsite generation failed: )r   r   r   r   r	   r   generate_sync_responseget_create_basic_html_sanitize_third_party_resources_write_file_asyncstr	Exception)r   r   website_datadeepsite_promptsetup_resultr#   r$   r%   ai_responser"   
index_filees               r   generate_websitezWebsiteService.generate_website   s     *	D+D11L 4LAAO "0!=!=======L&'9:K-? @AAL  0lKKKKKKKKJ !% F F& 8; 8 8 !G ! !      K y)) E*95#66|DD  ??MML%4J((\BBBBBBBBB !-* #L 1 1(	    	D 	D 	DB#a&&BBCCC	Ds   DD 
D:D55D:r-   c                 8   |                     dd          }|                     dd          }|                     dd          }|                     dd          }|                     dd          }d	| d
| d| d|rd| dnd d|rd| dnd d|rd| dnd dS )z/
        Create basic HTML as fallback
        websiteNamezRestaurant WebsitewebsiteDescriptionzWelcome to our restaurant!restaurantPhone restaurantEmailrestaurantAddressz
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>a  </title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 20px;
        }
        .description {
            font-size: 18px;
            color: #666;
            text-align: center;
            margin-bottom: 30px;
        }
        .contact-info {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 5px;
            margin-top: 30px;
        }
        .contact-item {
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>z%</h1>
        <p class="description">zf</p>
        
        <div class="contact-info">
            <h3>Contact Information</h3>
            z2<div class="contact-item"><strong>Phone:</strong> z</div>z
            z2<div class="contact-item"><strong>Email:</strong> z4<div class="contact-item"><strong>Address:</strong> z3
        </div>
    </div>
</body>
</html>
        )r'   )r   r-   website_namedescriptionphoneemailaddresss          r   r(   z!WebsiteService._create_basic_htmlJ   s1    $''7KLL"&&';=YZZ  !2B77  !2B77""#6;;: : :\ ]: :^ !,_: :f TY`O%OOOO^`g: :h TY`O%OOOO^`i: :j X_fSGSSSSdfk: : : :	r   r$   r#   c                 f  K   	 t          d          | dz  }|j                            d           |                                rt	          |          S |                     ||           d{V  t	          |          S # t          $ r$}t          dt	          |                     d}~ww xY w)z
        Create ZIP file from website folder asynchronously.

        Simple behavior: if downloads/<folder>.zip exists, reuse it; otherwise create it.
        	downloadsz.zipT)r   NzZIP creation failed: )r   parentr   existsr+   _create_zip_asyncr,   )r   r$   r#   zip_pathr2   s        r   
create_zipzWebsiteService.create_zip   s      	>K((k+?+?+??HO!!4!000    %8}}$ ((x@@@@@@@@@x==  	> 	> 	><CFF<<===	>s   AB *B 
B0B++B0c                t  K   	 t          |          }t          |          }t          |           d{V }|d         }t          |d                   }	 t	          ||           d{V  n# t
          $ r Y nw xY wd}d}d}	| j                            |d|           2 3 d{V }
|
                    d          sT|
                    d	          s?|
                    d
          s*|
                    d          s|
                    d          rd|
dW V   dS ||
z  }| j        	                    |          }|rmt          |          |	k    rZt          |          dk    rG||	d         }ddl}|                                dz  }||z
  dk    rd|dW V  |}t          |          }	6 | j        	                    |          }|r6|                     |          }|dz  }|                     ||           d{V  dd|dW V  dS # t
          $ r#}ddt          |           dW V  Y d}~dS d}~ww xY w)zK
        Generate website with streaming response for live preview
        Nr   r   r8   r   r   r   ErrorzConnection lostzUnable to connectzRequest timed outzAI service errorerror)typemessaged   i  i  r    )rJ   chunkr!   completezWebsite generation complete!)rJ   rK   folderzStreaming generation failed: )r   r   r   r   r	   r,   r   generate_streaming_response
startswith_extract_html_from_responselentimer)   r*   r+   )r   r   r-   r.   r/   r#   r$   accumulated_contentlast_render_timelast_sent_indexrM   r"   new_partrT   now
final_htmlr1   r2   s                     r   generate_website_streamz&WebsiteService.generate_website_stream   s     K	+D11L 4LAAO "0!=!=======L&'9:K-? @AAL&|\BBBBBBBBBB    #% O#JJ& 8; 8 8  K     @ @ @ @ @ @ @e
 ##G,, 0@0@AR0S0S W\WgWgh{W|W|   AF  AQ  AQ  Re  Af  Af   jo  jz  jz  {M  jN  jN  '#(      FF#u,#  $JJK^__ @C$5$5$G$G<((3..#/0@0@#A $"iikkD0!11D88(1)1# #     03,.1,.?.?O= B DDEXYYJ E!AA*MM
 *L8
,,ZDDDDDDDDD #9%         	 	 	C3q66CC           	sP   AH
 A, +H
 ,
A96H
 8A99(H
 !F,'A3H
 C,H
 

H7H22H7htmlc                     |s|S t          j        dd|t           j                  }d|vr.d|                                v rt          j        dd|d	          }|S )
zReplace known external kit/script tags with safer CDN links.

        Currently converts Font Awesome kit scripts to the stable cdnjs stylesheet.
        z[<script[^>]*src=[\"']https?:\\/\\/kit\.fontawesome\.com\/[\w.-]+\.js[\"'][^>]*>\s*</script>r8   )flagsz+cdnjs.cloudflare.com/ajax/libs/font-awesomez<head>z(?i)(<head[^>]*>)zt\1\n    <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css\">   )count)resub
IGNORECASElower)r   r\   s     r   r)   z.WebsiteService._sanitize_third_party_resources   s    
  	K vtvxz~  GI  GT  U  U  U 9DD4::<<''v2  5l  nr  z{  |  |  |r   	file_pathr    Nc                    K   t          j        |dd          4 d{V }|                    |           d{V  ddd          d{V  dS # 1 d{V swxY w Y   dS )z%Write content to file asynchronously.wzutf-8)encodingN)aiofilesopenwrite)r   re   r    fs       r   r*   z WebsiteService._write_file_async
  s      =C'BBB 	# 	# 	# 	# 	# 	# 	#a'''"""""""""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   A
AAsource_pathrE   c                 t   K   fd}t          j                                        d|           d{V  dS )zCreate ZIP file asynchronously.c                  *   t          j        dt           j                  5 }                     d          D ]A}|                                r+|                              }|                     ||           B	 d d d            d S # 1 swxY w Y   d S )Nrg   *)zipfileZipFileZIP_DEFLATEDrglobis_filerelative_tork   )zipfre   arcnamerm   rE   s      r   _create_zipz5WebsiteService._create_zip_async.<locals>._create_zip  s    30DEE 7!,!2!23!7!7 7 7I ((** 7"+"7"7"D"D

9g66677 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7s   ABBBN)asyncioget_event_looprun_in_executor)r   rm   rE   ry   s    `` r   rD   z WebsiteService._create_zip_async  s_      	7 	7 	7 	7 	7 	7 $&&66t[IIIIIIIIIIIr   )__name__
__module____qualname__r   r   r+   r   r3   r(   r   rF   r[   r)   r*   rD    r   r   r   r      s\       & & &
.D4S> .Dd38n .D .D .D .D`DtCH~ D# D D D DL>T > > > > > >,O$sCx. O O O ObC C    &# # # # # # #

J4 
J4 
JD 
J 
J 
J 
J 
J 
Jr   r   )__doc__typingr   r   ra   pathlibr   rq   sysrz   ri   pathinsertr+   __file__rB   services.website_data_utilsr   r   r   r	   services.ai_servicer
   r   r   r   r   <module>r      s   
         				        



   33ttH~~,344 5 5 5 q q q q q q q q q q q q ) ) ) ) ) )EJ EJ EJ EJ EJ EJ EJ EJ EJ EJr   