
    ~j0                    b   d Z ddlmZ ddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZ ddlmZ dd	lmZmZmZ  ej        e          Z ee                                          j        j        d
z  ZdZ e e ee                    ddd          Zddd-d$Zd.d%Zd/d(Z d0d*Z!d1d,Z"dS )2u  Brief HTML generator (Jinja2 with inline fallback).

Mirrors leads-importer's `notifier.py` philosophy — primary render path is a
Jinja2 template at `app/templates/brief.html.j2`; if the template is missing
or render fails, we fall back to a self-contained inline-HTML builder so a
template bug never blocks a brief from going to production.

The output HTML is deliberately mobile-first, single self-contained file
(inline CSS, no external stylesheet) with the canonical Open Graph tags from
the spec published in `README.md`.
    )annotationsN)datedatetime)Path)Any)EnvironmentFileSystemLoaderTemplateError)settings)PastShowShowVoiceProfile	templateszbrief.html.j2FT)loader
autoescapetrim_blockslstrip_blocks)recent_transcriptsshowr   voicer   
city_inteldict[str, Any]recent_newslist[dict[str, Any]]verified_factsremoved_facts
crowd_workcompetitor_warningsprevious_showPastShow | Nonehistorical_salesr   list[dict[str, Any]] | None
public_urlstrreturnc                   t          di t                      }	 t                              t                    } |j        di |}|r'|                                                    d          s)t          	                    d           t          |          S |S # t          t          f$ r4}t          	                    d|           t          |          cY d}~S d}~ww xY w)zARender the brief HTML. Falls back to inline HTML if Jinja breaks.<z8template returned empty/non-HTML; falling back to inlinez3template render failed (%s); falling back to inlineN )_build_template_contextlocals
_jinja_envget_template_TEMPLATE_NAMErenderstrip
startswithloggerwarning_inline_fallback_htmlr
   FileNotFoundError)r   r   r   r   r   r   r   r   r   r!   r   r#   contexttemplatehtmlexcs                   k/Users/abstractioneditor2/ai-workstation/projects/automation/show-city-briefing-agent/app/html/generator.pygenerate_brief_htmlr:   &   s      &1111G	.**>::x)))) 	24::<<22377 	2NNUVVV(111,- . . .LcRRR$W--------.s$   A=B B C+)CCCc                   g }|pg D ]W}|                     |                    dd          |                    dd          |                    dd          d           Xi d|j        d|j        d| j        d	| j        d
| j        dt          | j                  d| j                                        d| j	        d| j
        d|
dt          j        dt          j        d|pi d|pg d|pg d|pg d|pg |pg |||r|j        nd |r|j	        nd |r|j        nd |	pi t          j                                        d          dS )Ntitle urlpublished_at)r<   r>   r?   comediancomedian_idcitycountry	show_dateshow_date_shortshow_date_isovenuecapacityr#   og_cover_urlog_site_namer   r   r   r   r   z%Y-%m-%d %H:%M)r   recent_clipsr   previous_show_dateprevious_show_venueprevious_tickets_soldr!   generated_at)appendgetdisplay_namerA   rB   rC   rD   _short_date	isoformatrG   rH   r   OG_COVER_URLOG_SITE_NAMEtickets_soldr   nowstrftime)r   r   r   r   r   r   r   r   r   r!   r#   r   _kwargsrK   vs                  r9   r)   r)   C   s     L &B  UU7B''55##EE."55
 
 	 	 	 	
E&u( 		 	4<	
 	T^ 	;t~66 	1133 	 	DM 	j 	- 	- 	j&B 	{(b  	..B!" 	,"#$ 	j&B%&  38b$&9FPm55D6CM}22?L!V!;!;RV,2 //0@AA;       valuedate | Nonec                r    | sdS t          | d          r|                     d          nt          |           S )Nr=   rY   z
%b %-d, %Y)hasattrrY   r$   r]   s    r9   rS   rS   r   s;     r+25*+E+EU5>>,'''3u::Ur\   r5   c                   | d         }| d         }| d         }| d         }| d         }| d         }| d         }|                      dg           }d	}	t          |d
          D ]W\  }
}|                     dd	          }|                     dd	          }|	dt          |           dt          |           dz  }	Xd	}|                      dg           D ]T}|                     dd	          }|                     dd	          }|dt          |           dt          |           dz  }Ud	}|                      dg           D ]T}|                     dd	          }|                     dd	          }|dt          |           dt          |           dz  }Ud	                    g dt          |           dt          |           d| dt          |           dt          |           d| d| d| dt          |           dt          |           dt          |           dt          |           d t          |           d!| d"|	 d#| d$| d%          S )&uE   A bare-bones brief built inline — survives any template regression.rB   rC   rE   r@   r#   rI   rJ   r   r=      r<   anglez<li><strong>u   </strong> — z</li>r   claim
confidencez<li data-confidence="z">r   summaryz<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>u    — u
    Brief · z.</title>
  <meta property="og:title" content="u   ">
  <meta property="og:description" content="10 crowd work topics · local news · city intel">
  <meta property="og:image" content="z&">
  <meta property="og:url" content="zZ">
  <meta property="og:type" content="website">
  <meta property="og:site_name" content="a  ">
  <meta name="twitter:card" content="summary_large_image">
  <style>
    body { font: 16px/1.45 -apple-system, BlinkMacSystemFont, system-ui, sans-serif; color: #111; max-width: 720px; margin: 0 auto; padding: 16px; }
    h1 { font-size: 22px; margin: 0 0 4px 0; }
    h2 { font-size: 18px; margin: 24px 0 8px 0; border-bottom: 1px solid #eee; padding-bottom: 4px; }
    .meta { color: #666; font-size: 13px; margin-bottom: 16px; }
    ul { padding-left: 18px; }
    li { margin-bottom: 6px; }
  </style>
</head>
<body>
  <h1>z  Brief</h1>
  <div class="meta">z, u    · u3   </div>
  <h2>Crowd Work — Top 10</h2>
  <ol>
    z)
  </ol>
  <h2>Verified Facts</h2>
  <ul>z)</ul>
  <h2>Recent Local News</h2>
  <ul>z</ul>
</body>
</html>
)rQ   	enumerate_escjoin)r5   rB   rC   date_strr@   r#   coversiter   
work_itemsiidear<   rd   factsfre   confnewsnrg   s                        r9   r3   r3   x   s   6?Di G()Hz"H&JN#E>"D\2..JJZ++ S S4"%%"%%RT%[[RRURRRR

E[[)2.. J Jgr""uu\2&&IdIItE{{IIIID[[++ O Ogr""%%	2&&NtE{{NN$w--NNNN# # # # # #
 x..# # # #
  $Dzz# # # #
 6># # # # '+8nn# # # # <@::# # # # RZ# # # # ',# # # # %/# # # # +/t**# # # #. H~~/# # # #. !JJ/# # # #0 $ZZ1# # # #0 $(==1# # # #0 7?1# # # #6 7# # # #< =# # # #@ A# # # # # # #r\   r   c                    | dS t          |                               dd                              dd                              dd                              d	d
          S )z/Minimal HTML-escape for inline fallback output.Nr=   &z&amp;r'   z&lt;>z&gt;"z&quot;)r$   replacera   s    r9   ri   ri      sV    }rJJWS'""WS&!!WS&!!WS(##	%r\   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r   r   r"   r#   r$   r%   r$   )r%   r   )r]   r^   r%   r$   )r5   r   r%   r$   )r]   r   r%   r$   )#__doc__
__future__r   loggingr   r   pathlibr   typingr   jinja2r   r	   r
   
app.configr   
app.modelsr   r   r   	getLogger__name__r1   __file__resolveparent_TEMPLATE_DIRr-   r$   r+   r:   r)   rS   r3   ri   r(   r\   r9   <module>r      s  
 
 # " " " " "  # # # # # # # #             ? ? ? ? ? ? ? ? ? ?       3 3 3 3 3 3 3 3 3 3		8	$	$X&&((/6D [CC..//	  
( 7;. . . . . .:, , , ,^V V V V@ @ @ @F% % % % % %r\   