
    xj                        d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 dZ
dZ ed	           G d
 d                      ZddZddZdddddZdS )    )annotations)	dataclass)
Connection)record_audit_event)utc_now)	reviewdocxtestdemo	syntheticdryrunzdry runzwizard-testzwizard test)broker_import_reviewed_snapshotbroker_import_dry_rundocx_importwizard_testr
   r   r   r   T)frozenc                  Z    e Zd ZU ded<   ded<   ded<   ded<   ded<   edd	            Zd
S )CleanupPlan	list[str]account_idsinstrument_idstransaction_ids	alert_idsintmanual_position_countreturnc                ~    t          | j                  t          | j                  z   t          | j                  z   S )N)lenr   r   r   )selfs    e/home/agent/.hermes/worktrees/FinanceManager-sprint20h1-followup/src/jarvis_finance/equity/cleanup.pyartifact_countzCleanupPlan.artifact_count   s3    4#$$s4+>'?'??#dFZB[B[[[    N)r   r   )__name__
__module____qualname____annotations__propertyr!    r"   r    r   r      sv         \ \ \ X\ \ \r"   r   aliasstrcolumnsr   r   c                    g }|D ]+}t           D ]!}|                    d|  d| d| d           ",d                    |          pdS )Nzlower(coalesce(.z,'')) LIKE '%z%' OR 0)REVIEW_TOKENSappendjoin)r)   r+   partscoltokens        r    
_token_sqlr6      sw    E P P" 	P 	PELLN5NN3NNUNNNOOOO	P;;u$$r"   connr   c           	        t          dddg          }t          dddg          }|                     d| d| d                                          }t          d	 |D                       }d
                    d t
          D                       }|                     d| d|rd
                    d |D                       nd dg t
          |R                                           }t          d |D                       }|rmd
                    d |D                       }|                     d| d| dg ||R                                           }	t          d |	D                       }
ng }
g }|D ]}|                     d|r%dd
                    d |D                       z   dz   nd d|g|R                                           d         }t          |pd          dk    r|                    |           |
rrd
                    d |
D                       }|                     d | dt          |
                                                    }t          d! |D                       }ng }t          |                     d"                                          d         pd          }t          ||
|||          S )#a  Find clearly non-manual review/test/DOCX equity artifacts without exposing values.

    Conservative by design: manual_dashboard/manual_position_add data is excluded unless the
    account itself is clearly a review/test account. Crypto tables are never touched.
    aaccount_namenotespnamez}
        SELECT DISTINCT a.account_id
        FROM accounts a JOIN platforms p ON p.platform_id=a.platform_id
        WHERE (r.   z
)
        c                    h | ]
}|d          S )
account_idr(   .0rows     r    	<setcomp>z8identify_review_test_equity_artifacts.<locals>.<setcomp>1   s    DDD#l+DDDr"   ,c              3     K   | ]}d V  dS ?Nr(   rA   _s     r    	<genexpr>z8identify_review_test_equity_artifacts.<locals>.<genexpr>3   s"      "D"D13"D"D"D"D"D"Dr"   aU  
        SELECT DISTINCT t.transaction_id
        FROM transactions t
        JOIN instruments i ON i.instrument_id=t.instrument_id
        LEFT JOIN accounts a ON a.account_id=t.account_id
        WHERE lower(i.asset_class) IN ('stock','equity','etf')
          AND coalesce(t.is_voided,0)=0
          AND (
              t.source_type IN (a  )
              OR lower(coalesce(t.source_type,'')) LIKE '%review%'
              OR lower(coalesce(t.source_id,'')) LIKE '%review%'
              OR lower(coalesce(t.notes,'')) LIKE '%review%'
              OR lower(coalesce(t.notes,'')) LIKE '%docx%'
              OR lower(coalesce(t.notes,'')) LIKE '%synthetic%'
              OR lower(coalesce(t.notes,'')) LIKE '%demo%'
              OR lower(coalesce(t.notes,'')) LIKE '%test%'
              OR a.account_id IN (c              3     K   | ]}d V  dS rF   r(   rH   s     r    rJ   z8identify_review_test_equity_artifacts.<locals>.<genexpr>E   s"      +E+EAC+E+E+E+E+E+Er"   z''zN)
          )
          AND t.source_type NOT IN ('manual_dashboard')
        c                    h | ]
}|d          S )transaction_idr(   r@   s     r    rC   z8identify_review_test_equity_artifacts.<locals>.<setcomp>K   s    GGGc"23GGGr"   c              3     K   | ]}d V  dS rF   r(   rH   s     r    rJ   z8identify_review_test_equity_artifacts.<locals>.<genexpr>O   s"      66666666r"   z
            SELECT i.instrument_id
            FROM instruments i
            WHERE lower(i.asset_class) IN ('stock','equity','etf')
              AND EXISTS (SELECT 1 FROM transactions t WHERE t.instrument_id=i.instrument_id AND t.transaction_id IN (z))
              AND NOT EXISTS (
                  SELECT 1 FROM transactions t
                  WHERE t.instrument_id=i.instrument_id
                    AND coalesce(t.is_voided,0)=0
                    AND t.transaction_id NOT IN (z)
              )
            c                    h | ]
}|d          S )instrument_idr(   r@   s     r    rC   z8identify_review_test_equity_artifacts.<locals>.<setcomp>_   s     Q Q Q#_!5 Q Q Qr"   z}
            SELECT COUNT(*) AS c FROM transactions
            WHERE account_id=? AND coalesce(is_voided,0)=0
              zAND transaction_id NOT IN (c              3     K   | ]}d V  dS rF   r(   rH   s     r    rJ   z8identify_review_test_equity_artifacts.<locals>.<genexpr>j   s"      7U7U7U7U7U7U7U7Ur"   ) z
            cr   c              3     K   | ]}d V  dS rF   r(   rH   s     r    rJ   z8identify_review_test_equity_artifacts.<locals>.<genexpr>r   s"      7713777777r"   z0SELECT alert_id FROM alerts WHERE entity_id IN (c                    h | ]
}|d          S )alert_idr(   r@   s     r    rC   z8identify_review_test_equity_artifacts.<locals>.<setcomp>t   s    BBBC
OBBBr"   a)  
        SELECT COUNT(*) AS c
        FROM transactions t JOIN instruments i ON i.instrument_id=t.instrument_id
        WHERE lower(i.asset_class) IN ('stock','equity','etf')
          AND coalesce(t.is_voided,0)=0
          AND t.source_type IN ('manual_dashboard','vue_manual_position')
        )r6   executefetchallsortedr2   REVIEW_SOURCE_TYPESfetchoner   r1   tupler   )r7   account_whereplatform_whereaccount_rowsr   source_placeholderstx_rowsr   tx_phinstrument_rowsr   removable_accountsr?   	remaininginst_ph
alert_rowsr   r   s                     r    %identify_review_test_equity_artifactsri   "   s    s^W$=>>Mfg%677N<<	 	 	 $2	 	 	  hjj  DD|DDDEEK(("D"D0C"D"D"DDDll	 "5	 	  JU#^388+E+E+E+E+E#E#E#EZ^!	 	 	( 	-
,,,+ , hjj- . GGwGGGHHO  66o66666,, x}	  38   1o000
 
 (** 	   Q Q Q Q QRR %'! 
2 
2
LL `ov,sxx7U7U_7U7U7U/U/UUX[[[tv  
 */**
 
 (**S	 y~A!##%%j111 ((7777777\\"_U\"_"_"_afguavavww  A  A  C  C
BBzBBBCC				! ! hjj! !   )>?IWlmmmr"   NzMVP Real UI Acceptance cleanup)plannoterj   CleanupPlan | Nonerk   c                  |pt          |           }t                      }|j        rId                    d |j        D                       }|                     d| d|||g|j        R            |j        rGd                    d |j        D                       }|                     d| d|g|j        R            |j        rGd                    d |j        D                       }|                     d| d|g|j        R            |j        rGd                    d	 |j        D                       }|                     d
| d|g|j        R            t          | ddddi t          |j                  t          |j                  t          |j                  t          |j                  |j
        d|dd
  
        }|                                  |S )NrD   c              3     K   | ]}d V  dS rF   r(   rH   s     r    rJ   z3apply_review_test_equity_cleanup.<locals>.<genexpr>   s"      88ac888888r"   zUPDATE transactions SET is_voided=1, voided_at=?, void_reason=?, voided_by='dashboard_cleanup', updated_at=? WHERE transaction_id IN (rR   c              3     K   | ]}d V  dS rF   r(   rH   s     r    rJ   z3apply_review_test_equity_cleanup.<locals>.<genexpr>   s"      22ac222222r"   zFUPDATE alerts SET status='resolved', resolved_at=? WHERE alert_id IN (c              3     K   | ]}d V  dS rF   r(   rH   s     r    rJ   z3apply_review_test_equity_cleanup.<locals>.<genexpr>   s"      77ac777777r"   zIUPDATE instruments SET is_active=0, updated_at=? WHERE instrument_id IN (c              3     K   | ]}d V  dS rF   r(   rH   s     r    rJ   z3apply_review_test_equity_cleanup.<locals>.<genexpr>   s"      44ac444444r"   zCUPDATE accounts SET is_active=0, updated_at=? WHERE account_id IN (runtime_cleanup$archive_review_test_equity_artifacts
runtime_dbequity_review_test_artifacts)accounts_archivedinstruments_archivedtransactions_voidedalerts_resolvedmanual_positions_preservedTdashboard_cleanup)	sourceactionentity_type	entity_id
old_values
new_valuesuser_text_note	confirmed
created_by)ri   r   r   r2   rX   r   r   r   r   r   r   commit)r7   rj   rk   nowphaudit_ids         r     apply_review_test_equity_cleanupr      s`   >8>>D
))C 
XX884#788888 [  VX  [  [  [$3d233	
 	
 	
 ~ 
XX224>22222ZUWZZZ"4>""	
 	
 	
  EXX774#677777facfffil  iDos  pC  iD  iD  	E  	E  	E |XX444#344444`[]```cfbzimiybzbz{{{! 5 0!$T%5!6!6$'(;$<$<#&t';#<#<"4>22*.*D
 
 &!  H$ 	KKMMMOr"   )r)   r*   r+   r   r   r*   )r7   r   r   r   )r7   r   rj   rl   rk   r*   r   r*   )
__future__r   dataclassesr   sqlite3r   jarvis_finance.audit.logr   jarvis_finance.imports.commonr   r0   r[   r   r6   ri   r   r(   r"   r    <module>r      s   " " " " " " ! ! ! ! ! !       7 7 7 7 7 7 1 1 1 1 1 1r X  $	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\% % % %_n _n _n _nD VZ  hH ( ( ( ( ( ( ( (r"   