<?php
declare(strict_types=1);

namespace Autogedal\Manufacturers\Controller\Adminhtml\Page;

use Autogedal\Manufacturers\Api\ManufacturerPageRepositoryInterface;
use Autogedal\Manufacturers\Controller\Adminhtml\AbstractPage;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\NoSuchEntityException;

class Edit extends AbstractPage
{
    public function __construct(
        Context $context,
        private readonly ManufacturerPageRepositoryInterface $pageRepository
    ) {
        parent::__construct($context);
    }

    public function execute(): ResultInterface
    {
        $id = $this->getRequest()->getParam('id');

        if ($id) {
            try {
                $this->pageRepository->getById($id);
            } catch (NoSuchEntityException) {
                $this->messageManager->addErrorMessage(__('This page no longer exists.'));

                return $this->resultRedirectFactory->create()->setPath('*/*/');
            }
        }

        return $this->createPage(__('Manufacturer Page'));
    }
}
