<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ShopConfigType;
use CpCreation\VitiCore\Shop\Model\ShopConfig;
use CpCreation\VitiCore\Shop\Repository\ShopConfigRepository;
use Symfony\Component\Form\FormInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;

#[AsLiveComponent('admin:ShopConfigForm', template: '@CpCreationVitiCore/admin/shop/config/live_form.html.twig')]
final class ShopConfigForm extends AbstractAdminFormComponent
{
    public function __construct(
        private readonly ShopConfigRepository $shopConfigRepository,
    ) {
    }

    protected function instantiateAdminForm(): FormInterface
    {
        return $this->createForm(ShopConfigType::class, $this->shopConfigRepository->findOne() ?? new ShopConfig());
    }

    protected function saveData(mixed $data): void
    {
        if (!$data instanceof ShopConfig) {
            throw new \LogicException(sprintf('Expected "%s" data.', ShopConfig::class));
        }

        $this->shopConfigRepository->save($data);
    }

    protected function getSuccessRoute(): string
    {
        return 'admin_shop_config';
    }

    protected function getSuccessMessage(): string
    {
        return 'La configuration boutique a été enregistrée avec succès';
    }
}
