Automation challenge

Looking for help to achieve the following goal:
( not asking for a fish, asking for help to learn to fish)

Here is the scenario:

I have a Sleep Number bed with a Flexbase that I have integrated into HA.
I have 5 scripts that change the right side head of the bed to Position 0, 5, 10, 15 and 20
I built an ESPHome Sound Sensor and integrated it into HA.

My partner snores excessively but changing the position of the head of the bed stops the snoring for a while.

My automation should do the following:
Sound Sensor picks up snoring → Run Script 1
wait at least one minute
Sound Sensor picks up snoring again → Script 2
wait at least one minute
Sound Sensor picks up snoring again → Script 3
wait at least one minute
Sound Sensor picks up snoring again → Script 4
wait at least one minute
Sound Sensor picks up snoring again → Script 5
wait at least one minute

…now reverse the script order. Next snoring occurance → script 4
then Script 3 etc.

After the sequence is back to Script 1 i want to continously run this sequence.

So every time snoring is detected it would run through 1-2-3-4-5-4-3-2-1-2-3-4-5 etc etc.
Once condition I want to set is that the bed has to be occupied for this automation to run.
Once bed changes to bed not occupied the automation would stop.
Then restart again when bed is occupied again.

I have no idea where to start creating this. Maybe someone could help me along with the steps I need to take to build this without overwhelming me with an avalanche of information.

Thanks in advance !!