# Path: tlfp/tools/_step_logic.py
# def get_previous_step(steps, curr_step_index, is_old_procedure=False, get_depot_step=False):
#     # is_old_procedure: Budget, Financement Sécurité Sociale, lois organique
#     # get_depot_step: get the step the amendments are done on (including the depot steps)
# 
#     curr_step = steps[curr_step_index]
# 
#     if curr_step.get('stage') == 'l. définitive' and (
#             (curr_step.get('step') == 'hemicycle' and not get_depot_step) or
#             curr_step.get('step') == 'depot'
#         ):
#         # cf Constitution Article 45 alinéa 4 https://www.legifrance.gouv.fr/affichTexteArticle.do?idArticle=LEGIARTI000006527521&dateTexte=&categorieLien=cid
#         print('[step_logic] l. définitive / %s: fetching last AN hemi or last CMP commission' % curr_step.get('step'))
#         for i in reversed(range(curr_step_index)):
#             step = steps[i]
#             if step.get('echec'):
#                 continue
#             if step.get('stage') == 'CMP' and step.get('step') == 'commission':
#                 return i
#             if step.get('institution') == 'assemblee' and step.get('stage') == 'nouv. lect.' and step.get('step') == 'hemicycle':
#                 return i
#         else:
#             raise Exception('[step_logic] l. définitive / depot: no good text found, this should never happen !')
# 
#     cmp_hemi_failed = False
#     i = curr_step_index
#     while i > 0:
#         i -= 1
#         step = steps[i]
# 
#         if step.get('stage') == 'CMP' and step.get('step') == 'hemicycle':
#             # a CMP hemicycle previous step is the CMP commission
#             if curr_step.get('stage') == 'CMP' and curr_step.get('step') == 'hemicycle':
#                 continue
#             # if a CMP hemi fail, we mark it to ignore the commission text and the other CMP hemi
#             if step.get('echec'):
#                 print('[step_logic] CMP hemicycle failed, we ignore the CMP commission')
#                 cmp_hemi_failed = True
#             if cmp_hemi_failed:
#                 continue
# 
#         # the amendments are done on the depot text for a renvoi en commission
#         if step.get('echec') == 'renvoi en commission':
#             i -= 1 # we skip the commission step
#             continue
# 
#         if not step.get('echec'):
#             # for the old procedure, there's no text produced during the commission
#             if (is_old_procedure or use_old_procedure(step)) and step.get('step') == 'commission' and step.get('stage') != 'CMP':
#                 continue
# 
#             # if a CMP hemi fail, we ignore the CMP commission
#             if cmp_hemi_failed and step.get('stage') == 'CMP' and step.get('step') == 'commission' and curr_step.get('stage') != 'CMP':
#                 continue
# 
#             # do not take previous depot if it's not one of the initial depot
#             if not get_depot_step and step.get('step') == 'depot' and not is_one_of_the_initial_depots(steps, i):
#                 continue
# 
#             return i


import sys, os, json
from tlfp.tools._step_logic import get_previous_step
    return '_'.join((x for x in (str(i).zfill(2), steps[i].get('stage'), steps[i].get('institution'), steps[i].get('step','')) if x))


proc = json.load(open(os.path.join(TESTS_DIR, 'ressources/pjl12-688.json')))
steps = proc['steps']

# simple commission -> depot initial
assert get_previous_step(steps, 1) == 0

# lect. def. hemi -> nouv. lect AN hemicycle text
assert get_previous_step(steps, 16) == 11
# lect. def. hemi -> lect. def. depot (we want the real text number)
assert get_previous_step(steps, 16, get_depot_step=True) == 15
# lect. def. depot -> nouv. lect AN hemicycle text
assert get_previous_step(steps, 15) == 11
# lect. def. depot -> nouv. lect AN hemicycle text
assert get_previous_step(steps, 15, get_depot_step=True) == 11

# Aprés l'échec en commission qui a suivi le renvoi en commission, les amendements sont posés
# sur le texte de depot (le texte voté a l'AN)
assert get_previous_step(steps, 7, get_depot_step=True) == 3
# Pour la complétion des articles, il faut prendre le texte voté à l'AN ou celui du renvoi en comm
assert get_previous_step(steps, 7) == 2


proc = json.load(open(os.path.join(TESTS_DIR, 'ressources/ppl09-682.json')))
steps = proc['steps']

# (CMP hemicycle echec) Nouv. lect. AN com -> 2eme lect. AN hemi
assert get_previous_step(steps, 16, get_depot_step=True) == 15

# Path: tlfp/tools/_step_logic.py
# def get_previous_step(steps, curr_step_index, is_old_procedure=False, get_depot_step=False):
#     # is_old_procedure: Budget, Financement Sécurité Sociale, lois organique
#     # get_depot_step: get the step the amendments are done on (including the depot steps)
# 
#     curr_step = steps[curr_step_index]
# 
#     if curr_step.get('stage') == 'l. définitive' and (
#             (curr_step.get('step') == 'hemicycle' and not get_depot_step) or
#             curr_step.get('step') == 'depot'
#         ):
#         # cf Constitution Article 45 alinéa 4 https://www.legifrance.gouv.fr/affichTexteArticle.do?idArticle=LEGIARTI000006527521&dateTexte=&categorieLien=cid
#         print('[step_logic] l. définitive / %s: fetching last AN hemi or last CMP commission' % curr_step.get('step'))
#         for i in reversed(range(curr_step_index)):
#             step = steps[i]
#             if step.get('echec'):
#                 continue
#             if step.get('stage') == 'CMP' and step.get('step') == 'commission':
#                 return i
#             if step.get('institution') == 'assemblee' and step.get('stage') == 'nouv. lect.' and step.get('step') == 'hemicycle':
#                 return i
#         else:
#             raise Exception('[step_logic] l. définitive / depot: no good text found, this should never happen !')
# 
#     cmp_hemi_failed = False
#     i = curr_step_index
#     while i > 0:
#         i -= 1
#         step = steps[i]
# 
#         if step.get('stage') == 'CMP' and step.get('step') == 'hemicycle':
#             # a CMP hemicycle previous step is the CMP commission
#             if curr_step.get('stage') == 'CMP' and curr_step.get('step') == 'hemicycle':
#                 continue
#             # if a CMP hemi fail, we mark it to ignore the commission text and the other CMP hemi
#             if step.get('echec'):
#                 print('[step_logic] CMP hemicycle failed, we ignore the CMP commission')
#                 cmp_hemi_failed = True
#             if cmp_hemi_failed:
#                 continue
# 
#         # the amendments are done on the depot text for a renvoi en commission
#         if step.get('echec') == 'renvoi en commission':
#             i -= 1 # we skip the commission step
#             continue
# 
#         if not step.get('echec'):
#             # for the old procedure, there's no text produced during the commission
#             if (is_old_procedure or use_old_procedure(step)) and step.get('step') == 'commission' and step.get('stage') != 'CMP':
#                 continue
# 
#             # if a CMP hemi fail, we ignore the CMP commission
#             if cmp_hemi_failed and step.get('stage') == 'CMP' and step.get('step') == 'commission' and curr_step.get('stage') != 'CMP':
#                 continue
# 
#             # do not take previous depot if it's not one of the initial depot
#             if not get_depot_step and step.get('step') == 'depot' and not is_one_of_the_initial_depots(steps, i):
#                 continue
# 
#             return i


# Path: tests/test_steps.py
import sys, os, json
from tlfp.tools._step_logic import get_previous_step

    return '_'.join((x for x in (str(i).zfill(2), steps[i].get('stage'), steps[i].get('institution'), steps[i].get('step','')) if x))


proc = json.load(open(os.path.join(TESTS_DIR, 'ressources/pjl12-688.json')))
steps = proc['steps']

# simple commission -> depot initial
assert get_previous_step(steps, 1) == 0

# lect. def. hemi -> nouv. lect AN hemicycle text
assert get_previous_step(steps, 16) == 11
# lect. def. hemi -> lect. def. depot (we want the real text number)
assert get_previous_step(steps, 16, get_depot_step=True) == 15
# lect. def. depot -> nouv. lect AN hemicycle text
assert get_previous_step(steps, 15) == 11
# lect. def. depot -> nouv. lect AN hemicycle text
assert get_previous_step(steps, 15, get_depot_step=True) == 11

# Aprés l'échec en commission qui a suivi le renvoi en commission, les amendements sont posés
# sur le texte de depot (le texte voté a l'AN)
assert get_previous_step(steps, 7, get_depot_step=True) == 3
# Pour la complétion des articles, il faut prendre le texte voté à l'AN ou celui du renvoi en comm
assert get_previous_step(steps, 7) == 2


proc = json.load(open(os.path.join(TESTS_DIR, 'ressources/ppl09-682.json')))
steps = proc['steps']

# (CMP hemicycle echec) Nouv. lect. AN com -> 2eme lect. AN hemi
assert get_previous_step(steps, 16, get_depot_step=True) == 15

assert get_previous_step(steps, 16) == 11