Ë
    ¼vëjÇ$  ã                  ó6  — d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZ ddlmZ  ee«      j%                  d	«      d
z  ZdZdZdd„Zefdd„Zefdd„Zddef	 	 	 	 	 dd„Zdefdd„Zd d!d„Zd"d„Zefd#d„Zd$d%d„Zd$d&d„Zefd'd„Z efd(d„Z!d)d„Z"y)*z¨Templates in a table: GalaxiQ's defaults in the master database, an organisation's own in its schema. Every
product reads both the same way. A file is only ever a seed.é    )ÚannotationsN)ÚPath)ÚAnyÚDictÚListÚOptionalÚUnioné   )Útemplate_from_dictÚtemplate_from_file)ÚTemplateÚsqlzcontent_templates.sqlÚpublicc                ó   — d| › d�S )Nú"z".content_templates© )Úschemas    ú^/var/www/html/strategist-ai/venv/lib/python3.12/site-packages/galaxiq_utils/templates/store.pyÚ_tabler      s   € ØˆvˆhÐ)Ð*Ð*ó    c                ón   — | j                  t        j                  d¬«      j                  d|«      «       y )Nzutf-8)Úencodingz{schema})ÚexecuteÚSQLÚ	read_textÚreplace©Úconnr   s     r   Úensure_template_tabler      s&   € Ø‡L�L”—‘¨�Ó0×8Ñ8¸ÀVÓLÕMr   c                óV  — t        | |«       | j                  dt        |«      › d�|j                  |j                  |j
                  |j                  |j                  «       |j                  t        j                  t        |«      «      f«      j                  «       }t        |d   «      S )zøUpsert by (channel, name, version): the same template saved twice changes nothing but updated_at. Returns
    the row id. What the template fills is derived from its blocks and stored beside it, so a gallery can say it
    without reading the spec.úINSERT INTO a   (name, channel, kind, owner, spec, version, fills) VALUES (%s, %s, %s, %s, %s::jsonb, %s, %s::jsonb) ON CONFLICT (channel, name, version) DO UPDATE SET spec = EXCLUDED.spec, kind = EXCLUDED.kind, owner = EXCLUDED.owner, fills = EXCLUDED.fills, updated_at = now() RETURNING idr   )r   r   r   ÚnameÚchannelÚkindÚownerÚmodel_dump_jsonÚversionÚjsonÚdumpsÚfills_ofÚfetchoneÚstr)r   Útemplater   Úrows       r   Úsave_templater/      s—   € ô ˜$ Ô'Ø
�,‰,Ø
”v˜f“~Ð&ð 'Bð 	Bð 
�‰˜×(Ñ(¨(¯-©-¸¿¹È×IaÑIaÓIcÐem×euÑeuÜ	�‰”H˜XÓ&Ó	'ð	)ó	*÷
 +3©(«*ð ô ˆs�1‰v‹;Ðr   ÚgalaxiqÚemailc           
     ó¦   — t        | |«       | j                  dt        |«      › d�||||t        ||f«      j	                  «       }t        |d   «      S )z×A template that is a picture: what Media lists and what the design is read from when it is picked. Upsert by
    (channel, name, PICTURE_VERSION); the same name saved again takes the new picture. Returns the row id.r!   am   (name, channel, kind, owner, spec, version, image_url, description, listed) VALUES (%s, %s, %s, %s, NULL, %s, %s, %s, true) ON CONFLICT (channel, name, version) DO UPDATE SET kind = EXCLUDED.kind, owner = EXCLUDED.owner, spec = NULL, image_url = EXCLUDED.image_url, description = EXCLUDED.description, listed = true, enabled = true, updated_at = now() RETURNING idr   )r   r   r   ÚPICTURE_VERSIONr+   r,   )	r   r"   r$   ÚdescriptionÚ	image_urlr%   r#   r   r.   s	            r   Úsave_picture_templater6   )   sg   € ô ˜$ Ô'Ø
�,‰,Ø
”v˜f“~Ð&ð 'Mð 	Mð 
ˆw˜˜e¤_°iÀÐMóO÷
 PXÉxËzð ô ˆs�1‰v‹;Ðr   c                ó„   — t        | |«       | j                  dt        |«      › d�||t        |«      f«      j                  S )z»Whether a gallery shows these block designs. A picture template of the same name is not touched: it is the
    template; the design is what the art director keeps, unlisted, not disabled.úUPDATE z_ SET listed = %s, updated_at = now() WHERE channel = %s AND name = ANY(%s) AND spec IS NOT NULL)r   r   r   ÚlistÚrowcount)r   ÚnamesÚlistedr#   r   s        r   Ú
set_listedr=   7   sJ   € ô ˜$ Ô'Ø�<‰<˜'¤&¨£.Ð!1ð  2Qð  RØ ¬$¨u«+Ð6ó8ß8@¹ðAr   c           	     ó*  — |r|t         k7  r|gng t         gz   D ]x  }t        | |«      sŒt        | |«       | j                  dt	        |«      › d�||||f«      j                  «       }|sŒPt        |d   «      |d   |d   |d   |d   |d   |d	   d
œc S  y)a	  The picture template a reference names, by row id or by name (case does not matter): the organisation's own
    first when a schema is given, then the platform's. On one channel, or on any when channel is None. None when the
    reference names no picture template.zCSELECT id, name, kind, owner, image_url, description, channel FROM z­ WHERE (%s::text IS NULL OR channel = %s) AND enabled AND spec IS NULL AND image_url IS NOT NULL AND (id::text = %s OR lower(name) = lower(%s)) ORDER BY version DESC LIMIT 1r   r
   é   é   é   é   é   )Úidr"   r$   r%   r5   r4   r#   N)ÚPLATFORMÚ_existsr   r   r   r+   r,   )r   Ú	referencer#   r   Úwherer.   s         r   Úpicture_templaterI   ?   s×   € ñ %¨´8Ò);�6‘(ÀÌÀzÑQò 'ˆÜ�t˜UÔ#ØÜ˜d EÔ*Ø�l‰lØQÔRXÐY^ÓR_ÐQ`ð a[ð \ð �g˜y¨)Ð4ó	6÷ 7?±h³jð	 	ò
 Ü˜c !™f›+¨s°1©v¸sÀ1¹vÐPSÐTUÑPVÐehÐijÑekÐ|ð  ABñ  }CØ" 1™vñ'ò 'ð'ð r   c                ón  — i }| j                  «       D ]Ÿ  }dddddœj                  |j                  |j                  «      }|j                  j	                  d«      \  }}}||j                  |ddfv r|n|› d|› �j                  dd«      }|j                  |g «      }||vsŒ�|j                  |«       Œ¡ |S )	a  What a template fills, by who fills it, in plain words taken from the value keys themselves: {"person":
    ["first name", "cart items", "cart total"], "journey": ["code", "link"], ...}. Nothing is listed by hand; a new
    block's values appear here the moment it is in the contract.ÚpersonÚwrittenÚpicture)ÚidentityÚ	behaviourÚcopyÚmadeú.rP   ú Ú_)ÚvaluesÚgetÚsourceÚkeyÚ	partitionr   Ú
setdefaultÚappend)	r-   ÚoutÚvÚwhoÚgrouprT   ÚtailÚwordÚwordss	            r   r*   r*   R   sÄ   € ð !#€CØ�_‰_Óò ˆØ#°(ÀIÐW`Ña×eÑeÐfg×fnÑfnÐpq×pxÑpxÓyˆØŸ™Ÿ™¨Ó-‰ˆˆq�$à !§(¡(¨C°¸6Ð!BÑB‘È5È'ÐQRÐSWÐRXÐHY×bÑbÐcfÐhkÓlˆØ—‘˜s BÓ'ˆØ�uÒØ�L‰L˜Õðð €Jr   c                ót   — t        | |«       | j                  dt        |«      › d�||||f«      j                  S )z6The picture a gallery shows for this template version.r8   zY SET image_url = %s, updated_at = now() WHERE channel = %s AND name = %s AND version = %s)r   r   r   r:   )r   r"   r#   r'   r5   r   s         r   Úset_template_imagerd   b   sF   € ä˜$ Ô'Ø�<‰<˜'¤&¨£.Ð!1ð  2Kð  LØ" G¨T°7Ð;ó=ß=E¹XðFr   c                ó  — i }t         g|r|t         k7  r|gng z   D ]Ö  }t        | |«      sŒt        | |«       | j                  dt	        |«      › d|rdnd› d�|f«      j                  «       }|D ]�  \
  }}}	}
}}}}}}t        |«      ||	|
||t        |«      t        |t        «      r|n|rt        j                  |«      ni |t        |t        «      r|n|rt        j                  |«      ng dœ
||<   Œƒ ŒØ t        |j                  «       «      S )zÜWhat a gallery lists: the newest version of each name with its picture and what it fills. The platform's,
    then the organisation's own when a schema is given. The query a consumer reading the table directly would run.zšSELECT DISTINCT ON (name) id, name, kind, owner, version, image_url, fills, coalesce(description, spec->>'description'), spec->'suits', spec IS NULL FROM z WHERE channel = %s AND listed úAND enabledÚ ú ORDER BY name, version DESC)
rD   r"   r$   r%   r'   r5   rM   Úfillsr4   Úsuits)rE   rF   r   r   r   Úfetchallr,   ÚboolÚ
isinstanceÚdictr(   Úloadsr9   rU   )r   r#   r   Úenabled_onlyr\   rH   ÚrowsÚid_r"   r$   r%   r'   r5   ri   r4   rj   rM   s                    r   Úlist_template_cardsrs   i   s@  € ð &(€CÜ�©6°fÄÒ6H ™xÈbÑQò JˆÜ�t˜UÔ#ØÜ˜d EÔ*Ø�|‰|ð0Ü06°u³¨Ð>]Ño{Ñ^kð  BDð  ^Eð E*ð+à-4¨Jó8÷ 9A¹»
ð 	ð _cò 	JÑZˆC��t˜U G¨Y¸¸{ÈEÐSZÜ" 3›x°¸tÈeÐ`gÐvÜ$(¨£MÜ+5°e¼TÔ+B¡%Ñ^cÌÏÉÐTYÔIZÐikØ(3ÄzÐRWÔY]ÔG^¹eÑzÔei×eoÑeoÐpuÔevð  FHñJˆC�ŠIñ	JðJô �—
‘
“ÓÐr   c                ó\  — i }t         g|r|t         k7  r|gng z   D ]Ž  }t        | |«      sŒ| j                  dt        |«      › d|rdnd› d�|f«      j	                  «       }|D ]E  \  }t        |t        «      r|nt        j                  |«      }t        di |¤Ž}	|	||	j                  <   ŒG Œ� |S )z³Templates for a channel by name: the platform's defaults, then the organisation's own when a schema is given,
    which win on a shared name. Only the newest version of each name.z$SELECT DISTINCT ON (name) spec FROM z) WHERE channel = %s AND spec IS NOT NULL rf   rg   rh   r   )rE   rF   r   r   rk   rm   rn   r(   ro   r   r"   )
r   r#   r   rp   r\   rH   rq   ÚspecÚdatar-   s
             r   Úlist_templatesrw   }   sÕ   € ð  "€CÜ�©6°fÄÒ6H ™xÈbÑQò 	*ˆÜ�t˜UÔ#ØØ�|‰|Ø2´6¸%³=°/ÐAjñ  }IÑkxð  OQð  kRð R*ð *Ø,3¨:ó7ç7?±x³zð 	ð ò 	*‰GˆTÜ% d¬DÔ1‘4´t·z±zÀ$Ó7GˆDÜÑ' $Ñ'ˆHØ!)ˆC�—‘Òñ	*ð	*ð €Jr   c                óX   — | j                  dt        |«      › d�||f«      j                  S )Nr8   zI SET enabled = false, updated_at = now() WHERE channel = %s AND name = %s)r   r   r:   )r   r"   r#   r   s       r   Údisable_templatery   Ž   s0   € Ø�<‰<˜'¤&¨£.Ð!1Ð1zÐ{Ø  $˜ó)ß)1©ð2r   c                ó�   — d}t        t        |«      j                  d«      «      D ]  }t        | t	        |«      |«       |dz  }Œ |S )z\Every *.template.json in a folder saved as a GalaxiQ default. Returns how many were written.r   z*.template.jsonr
   )Úsortedr   Úglobr/   r   )r   Úfolderr   ÚnÚpaths        r   Úseed_templatesr€   “   sM   € à	€AÜ”t˜F“|×(Ñ(Ð):Ó;Ó<ò ˆÜ�dÔ.¨tÓ4°fÔ=Ø	ˆQ‰‰ðð €Hr   c                óH   — | j                  d|f«      j                  «       d uS )NzdSELECT 1 FROM information_schema.tables WHERE table_schema = %s AND table_name = 'content_templates')r   r+   r   s     r   rF   rF   œ   s)   € Ø�<‰<Ð~Ø˜	ó#ß#+¡8£:°Tð:ð :r   )r   r,   Úreturnr,   )r   r,   r‚   ÚNone)r-   r   r   r,   r‚   r,   )r"   r,   r$   r,   r4   r,   r5   r,   r%   r,   r#   r,   r   r,   r‚   r,   )
r;   z	List[str]r<   rl   r#   r,   r   r,   r‚   Úint)r1   N)rG   r,   r#   úOptional[str]r   r…   r‚   zOptional[Dict[str, Any]])r-   r   r‚   zDict[str, List[str]])r"   r,   r#   r,   r'   r„   r5   r,   r   r,   r‚   r„   )NT)r#   r,   r   r…   rp   rl   r‚   zList[Dict[str, Any]])r#   r,   r   r…   rp   rl   r‚   zDict[str, Template])r"   r,   r#   r,   r   r,   r‚   r„   )r}   zUnion[str, Path]r   r,   r‚   r„   )r   r,   r‚   rl   )#Ú__doc__Ú
__future__r   r(   Úpathlibr   Útypingr   r   r   r   r	   Úloadr   r   ru   r   Ú__file__Ú	with_namer   rE   r3   r   r   r/   r6   r=   rI   r*   rd   rs   rw   ry   r€   rF   r   r   r   ú<module>r�      sØ   ðñ@å "ã Ý ß 3Õ 3ç 8Ý á
ˆ8ƒn×Ñ˜uÓ%Ð(?Ñ?€Ø€ð €ó+ð /7ô Nð ;Cô ð foØ)0ÀðØ#&ðØ:=ðØMPóð ELÐ[cô Aôó&ð  ckô Fôô(ð" CKô 2ð
 BJô ô:r   