<?php

namespace App\Classes\Modules\Steps;

use App\Classes\Constants;
use App\Models\ModularStep;

class GenerateModularSteps
{
    public function execute() {
        foreach (Constants::ORDER_MODULAR_STEPS as $key=>$groups){
            $modularStep = new ModularStep;
            $modularStep->reference_id = $key;
            if($modularStep->save()){
                $this->createGroups($modularStep->id, $groups);
            }
        }

    }

    private function createGroups(string $modularId, array $groups){

        $modularStep = ModularStep::findOrFail($modularId);

        foreach ($groups as $array){

            $object = (new GroupStep($modularStep->id, $array['previous'], $array['next'], $array['steps']));

            $group = $modularStep->moduleGroups()->create([
                'previous' => $object->getPrevious(),
                'next' => $object->getNext()
            ]);

            $group->steps()->createMany($object->getSteps());
        }

    }


}