<?php

declare(strict_types=1);

namespace External\PompoBundle\Controller;

use External\PompoBundle\DRS\Synchronizer\OrderSynchronizer;
use External\PompoBundle\DRS\Synchronizer\POSOrderSynchronizer;
use External\PompoBundle\DRS\Synchronizer\PriceSynchronizer;
use External\PompoBundle\DRS\Synchronizer\StockSynchronizer;
use External\PompoBundle\DRS\Synchronizer\UserSynchronizer;
use External\PompoBundle\Util\PompoUtil;
use KupShop\AdminBundle\AdminRequiredControllerInterface;
use LSS\Array2XML;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/_drs")
 */
class DRSController extends AbstractController implements AdminRequiredControllerInterface
{
    /**
     * @Route("/sync/{type}/")
     */
    public function synchronize(PompoUtil $pompoUtil, string $type): Response
    {
        ini_set('max_execution_time', (string) (60 * 60 * 4));
        ini_set('memory_limit', '2048M');

        $pompoUtil->synchronizeDRS([$type]);

        return new JsonResponse(['success' => true]);
    }

    /**
     * @Route("/sync/prices/{productCode}/")
     */
    public function synchronizeProductPrices(PriceSynchronizer $priceSynchronizer, string $productCode): Response
    {
        $priceSynchronizer->processSingleProductPrices($productCode);

        return new JsonResponse(['success' => true]);
    }

    /**
     * @Route("/sync/stock/{productCode}/")
     */
    public function synchronizeProductStock(StockSynchronizer $stockSynchronizer, string $productCode): Response
    {
        $stockSynchronizer->syncItemsByCode($productCode);

        return new JsonResponse(['success' => true]);
    }

    /**
     * @Route("/sync/user/{drsId}/")
     */
    public function synchronizeDRSUser(UserSynchronizer $userSynchronizer, int $drsId): JsonResponse
    {
        $userSynchronizer->processDRSUser($drsId);

        return new JsonResponse(['success' => true]);
    }

    /**
     * @Route("/sync/pos_order/{drsId}/")
     */
    public function synchronizePOSOrder(POSOrderSynchronizer $orderSynchronizer, int $drsId, #[MapQueryParameter] bool $force = false): Response
    {
        $orderSynchronizer->processSingleItem($drsId, $force);

        return new JsonResponse(['success' => true]);
    }

    /**
     * @Route("/dump_order/{id}/")
     */
    public function dumpOrder(OrderSynchronizer $orderSynchronizer, int $id): Response
    {
        try {
            $order = \Order::get($id);
        } catch (\InvalidArgumentException $e) {
            throw new NotFoundHttpException('Order not found');
        }

        $response = new Response(
            Array2XML::createXML('root', $orderSynchronizer->getOrderData($order))->saveXML()
        );
        $response->headers->set('Content-Type', 'text/xml');

        return $response;
    }

    /**
     * @Route("/dump_user/{id}/")
     */
    public function dumpUser(UserSynchronizer $userSynchronizer, int $id): Response
    {
        $user = \User::createFromId($id);

        $response = new Response(
            Array2XML::createXML('document', $userSynchronizer->getUserData($user, null))->saveXML()
        );
        $response->headers->set('Content-Type', 'text/xml');

        return $response;
    }
}
