o
    3Jj                     @   sj   d Z ddlZddlZddlmZ ddlmZ dddZdd	 Zd
d Z	dd Z
dd Zdd Zdd ZdS )zMCommercial + compliance tools (stubbed for Block A; real logic in Block B/C).    N)Path)config	Outerwearc                    s   t jd d }| sddddS ttt|}d |r$ |d vr*ddddS  fd	d
|dd  D } fdd
|dd D }|rNt|t| nd}|rZt|t| n|}|ret	|| dnd}||dkrndnd| dg ddS )Ndemandztrends_sustainability.csv      ?stablestubdemand_indextrendsourcesustainable fashionr   c                    "   g | ]}|  rt|  qS  getfloat.0rcolr   ,/opt/greenlight/greenlight/tools/__init__.py
<listcomp>      " z#forecast_demand.<locals>.<listcomp>c                    r   r   r   r   r   r   r   r      r         ?risingGoogle Trends (real))r   zrecycled polyesterzorganic cotton)r
   r   categoryr   keywords)
r   DATAexistslistcsv
DictReaderopensumlenround)r"   pathrowsrecentprioravg_ravg_pidxr   r   r   forecast_demand	   s$   r4   c                  C   s(   t jd d } |  rt|  S i S )Nmarketzmarket_context.json)r   r$   r%   jsonloads	read_text)pr   r   r   market_context   s   r:   c                 C   s   t jd d }d}| r/tt|D ]}|dd | dd v r.t|d } nq| dd}| d	d}| d
d}|| }| dt	|d|t	|| dddS )Nmarginszdatac_apparel_benchmarks.csvgQ?r"    avg_profit_ratioretail_price_eurr   unit_cost_eurplanned_unitsskur   z!line economics + DataCo benchmark)rA   unit_margin_eurbenchmark_profit_ratioprojected_contribution_eurr   )
r   r$   r%   r'   r(   r)   r   lowerr   r,   )sku_rowbenchratiorowretailcostunits
margin_perr   r   r   project_margin$   s$    rN   c                 C   s   ddl }t|tr z||}W n |jy   ddd Y S w d}t|tr7|dp-g }|r7|d d}|du r@dddS | ||| k| |krO| | dS ddS )u8   Recompute claimed % vs TC — the calc tool for the wow.r   NF)verified_pctpassesproductscertifiedRecycledContentPct)claimed_pctrO   rP   gap)r6   
isinstancestrr7   JSONDecodeErrordictr   )rS   
tc_payload_jsoncoveredprodsr   r   r   verify_recycled_content9   s*   


r]   c                    s   |   s
ddddS ttt| }|r |d vr!ddddS  fdd|dd  D } fd	d|d
d D }|rEt|t| nd}|rQt|t| n|}|r\t|| dnd}||dkrhdddS dddS )Nr   r   r   r	   r   c                    r   r   r   r   columnr   r   r   V   r   z _trend_index.<locals>.<listcomp>r   c                    r   r   r   r   r^   r   r   r   W   r   r   r   r   r   r    r!   )r%   r&   r'   r(   r)   r*   r+   r,   )r-   r_   r.   r/   r0   r1   r2   r3   r   r^   r   _trend_indexP   s"   
r`   c                 C   s  t jd d }t jd d }| sg ddS t| dg }t }t }|p+g D ]*}|dp4|}|drA||d  |d	rV||d
|d	d	 f q,dd | dg D }	g }
|D ]l}|	|d
 }|srqft|dpyg }|d |vrqf|d |v rqf|d
 |d 	 f|v rqft
||d }|d dk rqf|
|d |d
 |d|d |d |d|d |d |d |d |dd|d d qf|
t|
d d!S )"zXScan ethical demand trends and match unclaimed line materials to substantiatable claims.r   zclaim_opportunities.jsonztrends_ethical.csvnone)opportunitiesr   mappingsmeta	attributetextrA   r<   c                 S   s   i | ]}|d  |qS )rA   r   )r   sr   r   r   
<dictcomp>r   s    z0discover_claim_opportunities.<locals>.<dictcomp>skus
attributesmaterial_attribute
claim_texttrend_columnr
   gRQ?opportunity_idname
claim_typeschemeevidence_attributetrend_keywordr   
uplift_pct   r   )rn   rA   sku_namerl   rp   rq   rr   rs   r
   r   rt   r   zGoogle Trends + line attributes)rb   countr   )r   r$   r%   r6   r7   r8   r   setaddrE   r`   appendr+   )linedeclared_claimsmapping_pathtrends_pathrc   declared_attrsdeclared_skus_claimedcrd   	sku_by_idoutmrF   attrsr   r   r   r   discover_claim_opportunitiesb   sZ   


 

r   c                 C   s   t | d d}||ddS )Ng{Gz?r   u7   UCPD/Omnibus ≤4% turnover for widespread infringement)max_exposure_eurblocked_claimsbasis)r,   )turnover_eurblocked_countcapr   r   r   compute_risk_exposure   s
   r   )r   )__doc__r'   r6   pathlibr   
greenlightr   r4   r:   rN   r]   r`   r   r   r   r   r   r   <module>   s    
1