<?php

namespace App\Controller\Admin;

use App\Entity\Configuration;
use App\Entity\IndividualData;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Lc\SovBundle\Field\StatusField;

class IndividualDataCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return IndividualData::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            TextField::new('firstname'),
            TextField::new('lastname'),
            EmailField::new('email'),
            TextField::new('introAnswer'),
            StatusField::new('status')->setRequired(false)
        ];
    }

    public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
    {
        $configurationRepository = $this->getDoctrine()->getRepository(Configuration::class);

        $configuration = $configurationRepository->findOneByDevAlias('intro_question');
        $entityInstance->setIntroQuestion($configuration->getValue());
        $entityManager->persist($entityInstance);
        $entityManager->flush();

        parent::persistEntity($entityManager, $entityInstance); // TODO: Change the autogenerated stub
    }
}
