
    EjG                     N   d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ej                  j                  ej                  j                  e            Zej                  j                  edd      Zej"                  j%                  dd	      Zexs d
Zej                  j                  ed      Zej                  j-                  e      r ee      5 ZeD ]y  Zej5                         Zesdev sej7                  d      r-ej9                  dd      \  ZZej"                  j?                  ej5                         ej5                                { 	 ddd        ed      Z ddl!Z!e j%                  e d       ed	      fde"fd       Z#e j%                  e d      d        Z$e jK                  e d      defd       Z&e jK                  e d      defd       Z'e jQ                  e e	ed      d       e)d k(  rddl*Z* e*jV                  d!d"d#d$       yy# 1 sw Y   xY w)%a  QuitSure Discovery -- FastAPI server.

Serves the static Next.js funnel build (web/out) under /discovery.
The funnel itself needs NO backend; this exists so the BE team's existing
setup (Apache reverse-proxy -> `uvicorn app:app`) keeps working unchanged,
and so Phase C can add Python API routes here without changing the deploy.

Run locally:   uvicorn app:app --reload --port 8081
Then open:     http://localhost:8081/   (mounts at / to match the strip-proxy)
    N)FastAPIQueryRequest)JSONResponse)StaticFilesweboutMOUNT_PREFIX /z.env=#   zQuitSure Discovery)titlez/api/me)defaultuc                 n    	 t        j                  |       }t        |xs i       S # t        $ r d}Y w xY w)zPersonalization lookup for the remarketing link ?u=<vHashedEmail>.
    Returns {} on miss/error so the funnel falls back to generic copy.N)dbget_profile	Exceptionr   )r   profiles     )/var/www/python/quitsure-discovery/app.pyapi_mer   /   s<    ..# 2&&  s   & 44z/api/configc                      t        t        j                  j                  d      xs dt        j                  j                  dd      dk(  d      S )zRuntime config for the funnel (so prod swaps the paywall via .env, no rebuild).
    Mirrors chatbot-onboarding's SUBSCRIPTION_URL.SUBSCRIPTION_URLNREDIRECT_TO_PAY01)subscriptionUrlredirectToPay)r   osenvironget     r   
api_configr&   :   sD     ::>>*<=E(93?3F  r%   z/api/track-batchrequestc                 ,  K   	 | j                          d{   }t        j                  |j                  d      |j                  d      xs g |j                  d      xs i       }t	        d|d      S 7 ^# t
        $ r t	        ddi      cY S w xY ww)	uI   Buffered clickstream events from the funnel → tbl_DiscoveryClickstream.N	sessionIdeventsmetaT)okinsertedr,   F)jsonr   insert_clickstreamr#   r   r   )r'   bodyns      r   api_track_batchr2   D   s     +\\^#!!$((;"7(9K9QrSWS[S[\bScSigij4Q788 $  +T5M**+s9   BA8 A6AA8 5B6A8 8BBBBz/api/sessionc                   K   	 | j                          d{   }t        j                  |j                  d      |j                  d      xs i       }t	        d|i      S 7 I# t
        $ r t	        ddi      cY S w xY ww)uW   Session summary upsert (answers, reached-offer, converted) → tbl_DiscoveryOnboarding.Nr)   datar,   F)r.   r   upsert_sessionr#   r   r   )r'   r0   r,   s      r   api_sessionr6   O   su     +\\^#txx4dhhv6F6L"MT2J'' $  +T5M**+s9   A?A# A!AA#  A?!A# #A<9A?;A<<A?T)	directoryhtml	discovery)name__main__zapp:appz0.0.0.0i  )hostportreload),__doc__r!   fastapir   r   r   fastapi.responsesr   fastapi.staticfilesr   pathdirnameabspath__file__BASE_DIRjoinOUT_DIRr"   r#   r
   MOUNT_AT	_env_pathexistsopen_ef_linestrip
startswithsplit_k_v
setdefaultappr   strr   r&   postr2   r6   mount__name__uvicornrunr$   r%   r   <module>r]      s  	 
 + + * +77??277??845
'',,x
. zz~~nb13 GGLL6*	77>>)	i >C 	>EKKMEe.>.>s.CS!,B

%%bhhj"((*=		>> () 
 L>	!""% 'c ' #' L>	%& ' \N*
+,+7 + -+ \N,
'(+w + )+ 		(K'=K	 P zGKK		TB > >s   H(H-H?AHH$