<?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\LocalizedException;

class Delete 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->deleteById($id);
                $this->messageManager->addSuccessMessage(__('You deleted the manufacturer page.'));
            } catch (LocalizedException $e) {
                $this->messageManager->addErrorMessage($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addExceptionMessage($e, __('Something went wrong while deleting the page.'));
            }
        }

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