# Path: tlfp/tools/detect_anomalies.py
# def find_anomalies(dossiers, verbose=True):
#     anomalies = 0
#     for dos in dossiers:
#         prev_step = ''
#         for step in dos['steps']:
#             step_name = ' • '.join((x for x in (step.get('stage'), step.get('institution'), step.get('step','')) if x))
#             if procedure.get(prev_step, {}).get(step_name, False) is False:
#                 if verbose:
#                     print('INCORRECT', prev_step, '->', step_name)
#                     print(dos.get('url_dossier_senat'), '|',dos.get('url_dossier_assemblee'))
#                     print()
#                 anomalies += 1
# 
#             #print(step_name, '      \t\t\t\t===>>', procedure.get(prev_step, {}).get(step_name))
# 
#             prev_step = step_name
# 
#     if verbose and anomalies:
#         print(anomalies, 'anomalies (', len(dossiers), 'doslegs)')
#     return anomalies
# 
# Path: tlfp/tools/_step_logic.py
# def should_ignore_commission_text(step, dos):
#     return step.get('step') == 'commission' and (
#         step.get('stage') == 'l. définitive' or (
#         use_old_procedure(step, dos) and step['institution'] in ('senat', 'assemblee'))
#     )
# 
# def use_old_procedure(step, dos=None):
#     if dos and dos.get('use_old_procedure'):
#         return True
#     return (step.get("enddate", step.get("date", "9999-99-99")) or "9999-99-99") < "2009-03-01"


import copy
from tlfp.tools.detect_anomalies import find_anomalies
from tlfp.tools._step_logic import should_ignore_commission_text, use_old_procedure
from lawfactory_utils.urls import validate_link_CC_decision




def fix_an_cmp_step_url(senat, an):
    # detect missing AN CMP step in senat data
    dos = copy.deepcopy(senat)

    # if CMP.assemblee.hemicycle is empty in senat data but ok in AN data
    # complete from AN data
    an_cmp_step_from_senat = [i for i, step in enumerate(senat['steps']) if
        step.get('stage') == 'CMP' and step.get('institution') == 'assemblee']
    an_cmp_step_from_an = [i for i, step in enumerate(an['steps']) if
        step.get('stage') == 'CMP' and step.get('institution') == 'assemblee']
    if an_cmp_step_from_an:
        if an_cmp_step_from_senat:
            dos['steps'][an_cmp_step_from_senat[0]]['source_url'] = \

# Path: tlfp/tools/detect_anomalies.py
# def find_anomalies(dossiers, verbose=True):
#     anomalies = 0
#     for dos in dossiers:
#         prev_step = ''
#         for step in dos['steps']:
#             step_name = ' • '.join((x for x in (step.get('stage'), step.get('institution'), step.get('step','')) if x))
#             if procedure.get(prev_step, {}).get(step_name, False) is False:
#                 if verbose:
#                     print('INCORRECT', prev_step, '->', step_name)
#                     print(dos.get('url_dossier_senat'), '|',dos.get('url_dossier_assemblee'))
#                     print()
#                 anomalies += 1
# 
#             #print(step_name, '      \t\t\t\t===>>', procedure.get(prev_step, {}).get(step_name))
# 
#             prev_step = step_name
# 
#     if verbose and anomalies:
#         print(anomalies, 'anomalies (', len(dossiers), 'doslegs)')
#     return anomalies
# 
# Path: tlfp/tools/_step_logic.py
# def should_ignore_commission_text(step, dos):
#     return step.get('step') == 'commission' and (
#         step.get('stage') == 'l. définitive' or (
#         use_old_procedure(step, dos) and step['institution'] in ('senat', 'assemblee'))
#     )
# 
# def use_old_procedure(step, dos=None):
#     if dos and dos.get('use_old_procedure'):
#         return True
#     return (step.get("enddate", step.get("date", "9999-99-99")) or "9999-99-99") < "2009-03-01"


# Path: tlfp/merge.py
import copy
from tlfp.tools.detect_anomalies import find_anomalies
from tlfp.tools._step_logic import should_ignore_commission_text, use_old_procedure
from lawfactory_utils.urls import validate_link_CC_decision





def fix_an_cmp_step_url(senat, an):
    # detect missing AN CMP step in senat data
    dos = copy.deepcopy(senat)

    # if CMP.assemblee.hemicycle is empty in senat data but ok in AN data
    # complete from AN data
    an_cmp_step_from_senat = [i for i, step in enumerate(senat['steps']) if
        step.get('stage') == 'CMP' and step.get('institution') == 'assemblee']
    an_cmp_step_from_an = [i for i, step in enumerate(an['steps']) if
        step.get('stage') == 'CMP' and step.get('institution') == 'assemblee']
    if an_cmp_step_from_an:
        if an_cmp_step_from_senat:
            dos['steps'][an_cmp_step_from_senat[0]]['source_url'] = \

                an['steps'][an_cmp_step_from_an[0]].get('source_url')