<?php

namespace KupShop\PreordersBundle\Controller;

use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Routing\TranslatedRoute;
use KupShop\PreordersBundle\Entity\Preorder;
use KupShop\PreordersBundle\Entity\UserPreorder;
use KupShop\PreordersBundle\Entity\UserPreorderMessage;
use KupShop\PreordersBundle\Service\PreorderExporter;
use KupShop\PreordersBundle\Service\PreorderUtil;
use KupShop\PreordersBundle\View\PreordersUpdateView;
use KupShop\PreordersBundle\View\PreordersView;
use KupShop\PreordersBundle\View\UserPreordersView;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class PreordersController extends AbstractController
{
    private UserContext $userContext;

    public function __construct(UserContext $userContext)
    {
        $this->userContext = $userContext;
    }

    #[TranslatedRoute(
        path: '/#preorders#/{id_date<\d+>?}',
        name: 'kupshop_preorders_show_active',
        methods: ['GET'],
    )]
    public function showPreorderDates(Request $request, PreordersView $view): Response
    {
        $uId = $this->userContext->getActiveId();
        if ($uId === null) {
            return $this->redirectToRoute('kupshop_user_login_login');
        }

        return $view->setRequest($request)
            ->getResponse();
    }

    #[TranslatedRoute(
        path: '/#preorders#/{id_date<\d+>?}',
        name: 'kupshop_preorder_update_items',
        methods: ['POST'],
    )]
    public function updatePreorderItems(Request $request, PreordersUpdateView $view): Response
    {
        $uId = $this->userContext->getActiveId();
        if ($uId === null) {
            return $this->redirectToRoute('kupshop_user_login_login');
        }

        $view->setRequest($request);

        return $view->getResponse();
    }

    #[TranslatedRoute(
        path: '/#account#/#preorders#/{id_date<\d+>?}',
        name: 'kupshop_preorders_show_user',
        methods: ['GET'],
    )]
    public function showUsersPreorders(Request $request, UserPreordersView $view): Response
    {
        $uId = $this->userContext->getActiveId();
        if ($uId === null) {
            return $this->redirectToRoute('kupshop_user_login_login');
        }

        $view->setRequest($request);

        return $view->getResponse();
    }

    #[TranslatedRoute(
        path: '/#account#/#preorders#/{id_date<\d+>}',
        name: 'kupshop_preorders_submit_message',
        methods: ['POST'],
    )]
    public function userPreorderSaveMessage(Request $request, int $id_date): Response
    {
        $uId = $this->userContext->getActiveId();
        if ($uId === null) {
            return $this->redirectToRoute('kupshop_user_login_login');
        }

        $message = $request->request->get('message');
        if (!$message || !is_string($message)) {
            throw new BadRequestException('Message text was not provided.');
        }

        $userPreorder = new UserPreorder((int) $uId, $id_date);
        $messages = $userPreorder->getMessages();

        $messages->add(new UserPreorderMessage(text: $message));
        if ($err = $messages->save()) {
            throw $err;
        }

        return $this->redirectToRoute('kupshop_preorders_show_user', ['id_date' => $id_date]);
    }

    #[TranslatedRoute(
        path: '/#preorders#/{id_date<\d+>}/export',
        name: 'kupshop_preorders_export_user',
    )]
    public function exportPreorder(int $id_date, Request $request, PreorderExporter $preorderExporter): Response
    {
        $user = $this->userContext->getActive();
        if ($user === null) {
            return $this->redirectToRoute('kupshop_user_login_login');
        }

        if (PreorderUtil::useMultipleDates() && $request->query->get('export_preorder')) {
            $preorder = new Preorder($id_date);
            $dateIds = array_column($preorder->getDates(), 'id');
        } else {
            $preorder = (new UserPreorder((int) $user->id, $id_date))->getPreorder();
            $dateIds = [$id_date];
        }

        $res = $preorderExporter->exportXLS(
            user: $user,
            preorder: $preorder,
            dates: $dateIds,
        );

        if (!$res) {
            throw new NotFoundHttpException('No pre-order items found.');
        }

        return new Response();
    }
}
