<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Workflow\WorkflowInterface;

class TaskLifecycleType extends AbstractType
{
    public function __construct(
        private WorkflowInterface $taskLifecycleStateMachine,
    ) {
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $choices = [];

        $places = $this->taskLifecycleStateMachine->getDefinition()->getPlaces();
        $metas = $this->taskLifecycleStateMachine->getMetadataStore();

        foreach ($places as $place => $id) {
            $meta = $metas->getPlaceMetadata($place);
            if (isset($meta['title'])) {
                $choices[$meta['title']] = $place;
            }
        }

        $resolver->setDefaults([
            'choices' => $choices,
        ]);
    }

    public function getParent(): string
    {
        return ChoiceType::class;
    }
}
