<?php

declare(strict_types=1);

namespace FiloBlu\ExtBlackbirdMenuManager\Controller\Adminhtml\Menu;

use FiloBlu\ExtBlackbirdMenuManager\Model\Menu\ExporterInterface;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;

/**
 *
 */
class Export extends Action
{
    /**
     * @var \FiloBlu\ExtBlackbirdMenuManager\Model\Menu\ExporterInterface
     */
    private $exporter;

    /**
     * @var \Magento\Framework\App\Response\Http\FileFactory
     */
    private $fileFactory;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \FiloBlu\ExtBlackbirdMenuManager\Model\Menu\ExporterInterface $exporter
     * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
     */
    public function __construct(
        Context $context,
        ExporterInterface $exporter,
        FileFactory $fileFactory
    ) {
        parent::__construct($context);
        $this->exporter = $exporter;
        $this->fileFactory = $fileFactory;
    }

    /**
     * @return \Magento\Framework\App\ResponseInterface
     * @throws \Exception
     */
    public function execute()
    {
        return $this->fileFactory->create(
            'menu.json',
            ['value' => $this->exporter->export([]), 'type' => 'string'],
            DirectoryList::ROOT,
            'application/json'
        );
    }
}