<?php

declare(strict_types=1);

namespace External\HannahBundle\Controller;

use External\HannahBundle\SAP\Util\SAPUtil;
use External\HannahBundle\Util\OCImportUtil;
use KupShop\AdminBundle\AdminRequiredControllerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;

class OCAdminController extends AbstractController implements AdminRequiredControllerInterface
{
    #[Route('/_oc/actions/import-product-positions/')]
    public function importProductPositions(OCImportUtil $importUtil): StreamedResponse
    {
        ini_set('max_execution_time', 60 * 60);

        sqlGetConnection()->getConfiguration()->setSQLLogger();

        return new StreamedResponse(fn () => $importUtil->importProductPositions(true));
    }

    #[Route('/_oc/actions/import-sales/')]
    public function importSales(SAPUtil $sapUtil): StreamedResponse
    {
        ini_set('max_execution_time', 60 * 60);

        sqlGetConnection()->getConfiguration()->setSQLLogger();

        return new StreamedResponse(function () use ($sapUtil) {
            // Clear output buffering
            while (ob_get_level()) {
                ob_end_clean();
            }

            $sapUtil->synchronizeSales(function () {
                echo '. ';
                flush();
            });

            echo 'Done';
        });
    }
}
