
     i                     z   d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	 ddl
mZ ej                  j                  d      dk(  Zd	d
ddddddZej                   j"                  ej$                  j"                  ej&                  j"                  hZd Zd Zd Z ederdnd        ej3                         D ]w  \  ZZ ej8                         5  ej:                  j=                         j?                  d      j                  e      Z e jB                  e jD                  e jF                  e jH                  e jJ                  dZ& ee e      \  Z'Z(e'r> ede de jR                  jT                   ddjW                  e'              	 ddd       ern ee       Z e jB                  e jD                  e jF                  e jH                  e jJ                  dZ, ede de jR                  jT                   de( de& d e, 
       n( ed!e de jR                  jT                   de( de&        ddd       z y# 1 sw Y   xY w)"a  
Repair Apr 29 BACKFILL AccountSyncLog rows that are stuck because default AI
report rows were intentionally not created for accounts that already had reports.

Dry run:
    UV_CACHE_DIR=/tmp/uv-cache uv run python manage.py shell < /tmp/repair_missing_ai_reports_backfill_20260429.py

Apply:
    UV_CACHE_DIR=/tmp/uv-cache APPLY=1 uv run python manage.py shell < /tmp/repair_missing_ai_reports_backfill_20260429.py
    N)transaction)AccountSyncLogReport)DEFAULT_AI_REPORT_TITLESrefresh_onboarding_run)
ReportTypeAPPLY1z$ca4026a5-9437-4c06-a776-9dce19351b31z$32fbf093-8c4e-46e6-8a78-ab07a79e1753z$12d63327-054d-453b-91b8-4cc8c79bb2c2z$58b3efa0-2977-4a66-9f35-8fc7f163acc5z$be48b448-3842-4209-8aba-5834a2af1c07z$92ab6eca-426e-4b73-9651-1d27cdd79132z$b6d79206-490c-479f-9b9c-182e47f7759c)z$a9190b48-d079-46d8-95d1-2be29f117d99z$6d89290e-b001-4e84-9666-4e63321c53acz$df2900d6-fcff-4667-9269-b4ae5ecd8177z$29ee93e2-c2da-42ba-a6c0-62abb6ced819z$3fd4b98a-f911-4a81-ae21-e57ae7f6832ez$f570e378-65f5-4cce-b168-782551022bc4z$f6621d27-ec8e-48e8-9000-c4c605522601c                     t         j                  j                  | j                  d      }|j                  t              j                         |j                  t              j                         dS )NF)account
is_deleted)type__in)	title__in)existing_bootstrap_reportsrequired_ai_reports)r   objectsfilterr    REPORT_TYPES_THAT_SKIP_BOOTSTRAPcountr   )runreportss     3/tmp/repair_missing_ai_reports_backfill_20260429.pyget_report_countsr   +   s_    nn##CKKE#JG&-nn5 '5 '

%'&~~.  .  

%'     c                    g }t        | j                        |k7  r!|j                  d| d| j                          | j                  t        j
                  j                  k7  r|j                  d| j                   d       | j                  t        j                  j                  k7  r|j                  d| j                   d       | j                  t        j                  j                  k(  r|j                  d       t        |       }|d   d	k  r|j                  d
       |d   d	k7  r|j                  d|d    d       ||fS )Nzaccount mismatch: expected z, got zsync_type is z, expected BACKFILLzstats_sync_status is z, expected successzalready successr   r   zMno existing regular/fatigue/fixer reports; this is not the short-circuit caser   zfound z4 required AI report rows; expected 0 for this repair)str
account_idappend	sync_typer   SyncTypeBACKFILLstats_sync_statusStageStatusSUCCESSoverall_statusOverallStatusr   )r   expected_account_idproblemscountss       r   validate_targetr*   7   s.   H
3>>1156I5J&QTQ_Q_P`ab
}}//888-6IJK
 : : B BB/0E0E/FFXYZ
^99AAA)*s#F*+q0gh#$)V1233gh	
 Vr   c                 ~    t         j                  j                  | _        | j	                  ddg       t        |       S )Nai_reports_status
updated_at)update_fields)r   r#   r$   r,   saver   )r   s    r   
repair_runr0   M   s7     +66>>CHH/>H?!#&&r   zMode: zDRY RUNr   )id)r%   current_stagead_creative_sync_statusr"   r,   zSKIP  z: z; z	REPAIRED z
: reports=z	, before=z, after=zWOULD REPAIR )-__doc__os	django.dbr   creative_module.modelsr   r   +creative_module.services.onboarding_servicer   r   creative_module.typesr   environgetr	   TARGET_RUNSREGULARvalueFATIGUEFIXERr   r   r*   r0   printitemsrun_idr'   atomicr   select_for_updateselect_relatedr   r%   r2   r3   r"   r,   beforer(   r)   r   ad_account_namejoinafter r   r   <module>rM      sW  	 
 ! 9 - 	

w3& -S,R,R,R,R,R,R $  	,' %wY/0 1#.#4#4#6F				""446^I&SFS^ 	 "00 ..'*'B'B!$!6!6!$!6!6
 +30CD&E&3;;#>#>"?r$))HBUAVWX# 
	& S/C"%"4"4!$!2!2+.+F+F%(%:%:%(%:%:E F81S[[%@%@$A B!()F88E7D
 xq)D)D(E F!()F85C 
	 $7		s   B7H0BH00H:	