Ë
    §‘ˆjç  ã                   ób   — d Z ddlZddlmZ ddlmZ  ej                  e«      ZdZ	de
fd„Zd„ Zy)	z÷Bands a tenant's own prices into budget, mid and premium.

"Premium" has no global meaning -- it means expensive for this catalog. The live
catalog spans 79 to 3,699,999 minor units, so a fixed threshold would put nearly
everything in one bucket.
é    N)Úsql)Úget_db_connectioné
   Ú	tenant_idc                 óØ  — t        «       }	 |j                  «       5 }|j                  t        j                  d«      j                  t        j                  | «      «      «       |j                  «       \  }}}ddd«       |j                  «        r|t        k  s�€t        j                  d| «       yt        |«      t        |«      fS # 1 sw Y   ŒUxY w# |j                  «        w xY w)z­The 33rd and 67th percentile of a tenant's prices, or None.

    Prefers price_reference_cents so a tenant with two currencies is not banded
    on unconverted numbers.
    a½  
                SELECT count(*),
                       percentile_disc(0.33) WITHIN GROUP (ORDER BY p),
                       percentile_disc(0.67) WITHIN GROUP (ORDER BY p)
                FROM (
                    SELECT COALESCE(price_reference_cents, price_cents) AS p
                    FROM {}.strategist_products
                    WHERE COALESCE(price_reference_cents, price_cents) IS NOT NULL
                ) prices
            Nz,Too few priced products in %s to band prices)r   ÚcursorÚexecuter   ÚSQLÚformatÚ
IdentifierÚfetchoneÚcloseÚMIN_PRICED_PRODUCTSÚloggerÚinfoÚint)r   ÚconnÚcurÚcountÚlowÚhighs         úB/var/www/html/strategist-ai/app/services/enrichment/price_tiers.pyÚcompute_bandsr      sÅ   € ô Ó€DðØ�[‰[‹]ð 	.˜cØ�K‰KœŸ™ð 	!ó 	÷ ‘œŸ™ yÓ1Ó2ô	4ð  #Ÿ|™|›~ÑˆE�3˜÷	.ð 	�
‰
Œá�EÔ/Ò/°3°;À$À,Ü�‰ÐBÀIÔNØÜˆs‹8”S˜“YÐÐ÷%	.ð 	.ûð 	�
‰
�ús#   ŒC œACÁ7C ÃCÃC ÃC)c                 ó0   — | �|€y |\  }}| |k  ry| |k  ryy)NÚbudgetÚmidÚpremium© )Úprice_centsÚbandsr   r   s       r   Úclassifyr!   /   s1   € ØÐ˜e˜mØØ�I€CˆØ�SÒØØ�TÒØØó    )Ú__doc__ÚloggingÚpsycopg2r   Úapp.services.infra.databaser   Ú	getLoggerÚ__name__r   r   Ústrr   r!   r   r"   r   ú<module>r*      s>   ðñó å å 9à	ˆ×	Ñ	˜8Ó	$€àÐ ð˜Só ó:r"   