<?php
namespace PortalPeople\DisabledPostcodes\Controller\Adminhtml\Postcode;

use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use PortalPeople\DisabledPostcodes\Api\DisabledPostcodeManagementInterface;

class Delete extends AbstractPostcode
{
    private DisabledPostcodeManagementInterface $management;

    public function __construct(
        Context $context,
        DisabledPostcodeManagementInterface $management
    ) {
        parent::__construct($context);
        $this->management = $management;
    }

    public function execute()
    {
        $id = (int)$this->getRequest()->getParam('id');
        try {
            if ($id > 0) {
                $this->management->delete($id);
                $this->messageManager->addSuccessMessage(__('The postcode rule has been deleted.'));
            }
        } catch (\Throwable $e) {
            $this->messageManager->addErrorMessage($e->getMessage());
        }

        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('disabledpostcodes/postcode/index');
    }
}
