<?php

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Admin;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

abstract class AbstractAdminFormComponent extends AbstractController
{
    use ComponentWithFormTrait;
    use DefaultActionTrait;

    abstract protected function instantiateAdminForm(): FormInterface;

    abstract protected function saveData(mixed $data): void;

    abstract protected function getSuccessRoute(): string;

    abstract protected function getSuccessMessage(): string;

    protected function instantiateForm(): FormInterface
    {
        return $this->instantiateAdminForm();
    }

    #[LiveAction]
    public function save(): RedirectResponse
    {
        $this->submitForm();

        $this->saveData($this->getForm()->getData());
        $this->addFlash('alert-success', $this->getSuccessMessage());

        return $this->redirectToRoute($this->getSuccessRoute());
    }
}
