<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\AdminBundle\Components\Edit;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\Attribute\PreReRender;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\LiveCollectionTrait;

abstract class EditForm extends AbstractController
{
    use ComponentToolsTrait;
    use DefaultActionTrait;
    use LiveCollectionTrait;

    #[LiveProp]
    public array $formOptions = [];

    #[LiveProp]
    public bool $formDirty = false;

    #[LiveProp]
    public array $sections = [];

    #[LiveProp]
    public ?string $section = null;

    #[LiveProp]
    public string $editRoute = 'admin_offers_edit';

    #[LiveProp]
    public string $viewRoute = 'admin_offers_proof';

    #[LiveProp]
    public array $routeParameters = [];

    public function getRouteParameters(): array
    {
        if ([] !== $this->routeParameters) {
            return $this->routeParameters;
        }

        if (isset($this->item) && method_exists($this->item, 'getUuid')) {
            return ['uuid' => $this->item->getUuid()->toBase58()];
        }

        return [];
    }

    #[PreReRender]
    public function formChangedHook(): void
    {
        $this->formDirty = true;
    }

    #[LiveAction]
    public function setDirty(): void
    {
        $this->formDirty = true;
    }
}
