<?php
/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */

declare(strict_types=1);

namespace PrestaShopBundle\Controller\Admin\Improve\International;

use Exception;
use PrestaShop\PrestaShop\Core\Domain\Zone\Command\BulkDeleteZoneCommand;
use PrestaShop\PrestaShop\Core\Domain\Zone\Command\BulkToggleZoneStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Zone\Command\DeleteZoneCommand;
use PrestaShop\PrestaShop\Core\Domain\Zone\Command\ToggleZoneStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Zone\Exception\CannotEditZoneException;
use PrestaShop\PrestaShop\Core\Domain\Zone\Exception\CannotToggleZoneStatusException;
use PrestaShop\PrestaShop\Core\Domain\Zone\Exception\DeleteZoneException;
use PrestaShop\PrestaShop\Core\Domain\Zone\Exception\MissingZoneRequiredFieldsException;
use PrestaShop\PrestaShop\Core\Domain\Zone\Exception\ZoneException;
use PrestaShop\PrestaShop\Core\Domain\Zone\Exception\ZoneNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Zone\Query\GetZoneForEditing;
use PrestaShop\PrestaShop\Core\Domain\Zone\QueryResult\EditableZone;
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Builder\FormBuilderInterface;
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Handler\FormHandlerInterface;
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\GridDefinitionFactoryInterface;
use PrestaShop\PrestaShop\Core\Grid\Definition\Factory\ZoneGridDefinitionFactory;
use PrestaShop\PrestaShop\Core\Grid\GridFactoryInterface;
use PrestaShop\PrestaShop\Core\Search\Filters\ZoneFilters;
use PrestaShopBundle\Controller\Admin\PrestaShopAdminController;
use PrestaShopBundle\Security\Attribute\AdminSecurity;
use PrestaShopBundle\Security\Attribute\DemoRestricted;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * ZoneController is responsible for handling "Improve > International > Locations > Zones"
 */
class ZoneController extends PrestaShopAdminController
{
    /**
     * Show all zones.
     *
     * @param Request $request
     * @param ZoneFilters $zoneFilters
     *
     * @return Response
     */
    #[AdminSecurity("is_granted('read', request.get('_legacy_controller'))")]
    public function indexAction(
        Request $request,
        ZoneFilters $zoneFilters,
        #[Autowire(service: 'prestashop.core.grid.factory.zone')]
        GridFactoryInterface $zoneGridFactory
    ): Response {
        $zoneGrid = $zoneGridFactory->getGrid($zoneFilters);

        return $this->render('@PrestaShop/Admin/Improve/International/Zone/index.html.twig', [
            'zoneGrid' => $this->presentGrid($zoneGrid),
            'enableSidebar' => true,
            'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
            'layoutHeaderToolbarBtn' => $this->getZoneToolbarButtons(),
        ]);
    }

    /**
     * Provides filters functionality.
     *
     * @param Request $request
     *
     * @return RedirectResponse
     */
    #[AdminSecurity("is_granted('read', request.get('_legacy_controller'))")]
    public function searchAction(
        Request $request,
        #[Autowire(service: 'prestashop.core.grid.definition.factory.zone')]
        GridDefinitionFactoryInterface $zoneGridDefinitionFactory
    ): RedirectResponse {
        return $this->buildSearchResponse(
            $zoneGridDefinitionFactory,
            $request,
            ZoneGridDefinitionFactory::GRID_ID,
            'admin_zones_index'
        );
    }

    /**
     * Show "Add new" zone form and handles its submit.
     *
     * @param Request $request
     *
     * @return Response
     */
    #[AdminSecurity("is_granted('create', request.get('_legacy_controller'))", redirectRoute: 'admin_zones_index', message: 'You need permission to create new zone.')]
    public function createAction(
        Request $request,
        #[Autowire(service: 'prestashop.core.form.identifiable_object.builder.zone_form_builder')]
        FormBuilderInterface $zoneFormBuilder,
        #[Autowire(service: 'prestashop.core.form.identifiable_object.handler.zone_form_handler')]
        FormHandlerInterface $zoneFormHandler
    ): Response {
        $zoneForm = $zoneFormBuilder->getForm();
        $zoneForm->handleRequest($request);

        try {
            $handleResult = $zoneFormHandler->handle($zoneForm);

            if (null !== $handleResult->getIdentifiableObjectId()) {
                $this->addFlash('success', $this->trans('Successful creation', [], 'Admin.Notifications.Success'));

                return $this->redirectToRoute('admin_zones_index');
            }
        } catch (Exception $e) {
            $this->addFlash('error', $this->getErrorMessageForException($e, $this->getErrorMessages($e)));
        }

        return $this->render('@PrestaShop/Admin/Improve/International/Zone/create.html.twig', [
            'zoneForm' => $zoneForm->createView(),
            'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
            'enableSidebar' => true,
            'layoutTitle' => $this->trans('New zone', [], 'Admin.Navigation.Menu'),
        ]);
    }

    /**
     * Displays zone edit for and handles its submit.
     *
     * @param int $zoneId
     * @param Request $request
     *
     * @return Response
     */
    #[AdminSecurity("is_granted('update', request.get('_legacy_controller'))", redirectRoute: 'admin_zones_index', message: 'You need permission to edit this.')]
    public function editAction(
        int $zoneId,
        Request $request,
        #[Autowire(service: 'prestashop.core.form.identifiable_object.builder.zone_form_builder')]
        FormBuilderInterface $formBuilder,
        #[Autowire(service: 'prestashop.core.form.identifiable_object.handler.zone_form_handler')]
        FormHandlerInterface $formHandler
    ): Response {
        try {
            /** @var EditableZone $editableZone */
            $editableZone = $this->dispatchQuery(new GetZoneForEditing($zoneId));

            $zoneForm = $formBuilder->getFormFor($zoneId);
            $zoneForm->handleRequest($request);

            $result = $formHandler->handleFor($zoneId, $zoneForm);

            if ($result->isSubmitted() && $result->isValid()) {
                $this->addFlash('success', $this->trans('Update successful', [], 'Admin.Notifications.Success'));

                return $this->redirectToRoute('admin_zones_index');
            }
        } catch (Exception $e) {
            $this->addFlash('error', $this->getErrorMessageForException($e, $this->getErrorMessages($e)));

            if ($e instanceof ZoneNotFoundException) {
                return $this->redirectToRoute('admin_zones_index');
            }
        }

        return $this->render('@PrestaShop/Admin/Improve/International/Zone/edit.html.twig', [
            'zoneName' => $editableZone->getName(),
            'zoneForm' => $zoneForm->createView(),
            'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
            'enableSidebar' => true,
            'layoutTitle' => $this->trans(
                'Editing zone %name%',
                [
                    '%name%' => $editableZone->getName(),
                ],
                'Admin.Navigation.Menu'
            ),
        ]);
    }

    /**
     * Deletes zone.
     *
     * @param int $zoneId
     *
     * @return RedirectResponse
     */
    #[DemoRestricted(redirectRoute: 'admin_zones_index')]
    #[AdminSecurity("is_granted('delete', request.get('_legacy_controller'))", redirectRoute: 'admin_zones_index', message: 'You need permission to delete this.')]
    public function deleteAction(int $zoneId): RedirectResponse
    {
        try {
            $this->dispatchCommand(new DeleteZoneCommand($zoneId));
            $this->addFlash('success', $this->trans('Successful deletion', [], 'Admin.Notifications.Success'));
        } catch (ZoneException $e) {
            $this->addFlash('error', $this->getErrorMessageForException($e, $this->getErrorMessages($e)));

            return $this->redirectToRoute('admin_zones_index');
        }

        return $this->redirectToRoute('admin_zones_index');
    }

    /**
     * Toggles zone active status.
     *
     * @param int $zoneId
     *
     * @return RedirectResponse
     */
    #[DemoRestricted(redirectRoute: 'admin_zones_index')]
    #[AdminSecurity("is_granted('update', request.get('_legacy_controller'))", redirectRoute: 'admin_zones_index', message: 'You do not have permission to edit this.')]
    public function toggleStatusAction(int $zoneId): RedirectResponse
    {
        try {
            $this->dispatchCommand(new ToggleZoneStatusCommand($zoneId));
            $this->addFlash(
                'success',
                $this->trans('The status has been successfully updated.', [], 'Admin.Notifications.Success')
            );
        } catch (Exception $e) {
            $this->addFlash('error', $this->getErrorMessageForException($e, $this->getErrorMessages($e)));
        }

        return $this->redirectToRoute('admin_zones_index');
    }

    /**
     * Deletes zones in bulk action
     *
     * @param Request $request
     *
     * @return RedirectResponse
     */
    #[DemoRestricted(redirectRoute: 'admin_zones_index')]
    #[AdminSecurity("is_granted('delete', request.get('_legacy_controller'))", redirectRoute: 'admin_zones_index')]
    public function bulkDeleteAction(Request $request): RedirectResponse
    {
        $zoneIds = $this->getBulkZonesFromRequest($request);

        try {
            $this->dispatchCommand(new BulkDeleteZoneCommand($zoneIds));

            $this->addFlash(
                'success',
                $this->trans('The selection has been successfully deleted.', [], 'Admin.Notifications.Success')
            );
        } catch (ZoneException $e) {
            $this->addFlash('error', $this->getErrorMessageForException($e, $this->getErrorMessages($e)));
        }

        return $this->redirectToRoute('admin_zones_index');
    }

    /**
     * Bulk toggles zones status.
     *
     * @param string $status
     * @param Request $request
     *
     * @return RedirectResponse
     */
    #[DemoRestricted(redirectRoute: 'admin_zones_index')]
    #[AdminSecurity("is_granted('update', request.get('_legacy_controller'))", redirectRoute: 'admin_zones_index')]
    public function bulkToggleStatus(string $status, Request $request): RedirectResponse
    {
        $status = $status === 'enable';
        $zoneIds = $this->getBulkZonesFromRequest($request);

        try {
            $this->dispatchCommand(new BulkToggleZoneStatusCommand($status, $zoneIds));
            $this->addFlash(
                'success',
                $this->trans('The status has been successfully updated.', [], 'Admin.Notifications.Success')
            );
        } catch (ZoneException $e) {
            $this->addFlash('error', $this->getErrorMessageForException($e, $this->getErrorMessages($e)));
        }

        return $this->redirectToRoute('admin_zones_index');
    }

    /**
     * Returns zone error messages mapping.
     *
     * @param Exception $e
     *
     * @return array
     */
    private function getErrorMessages(Exception $e): array
    {
        return [
            CannotEditZoneException::class => $this->trans(
                'An error occurred while editing the zone.',
                [],
                'Admin.International.Notification'
            ),
            MissingZoneRequiredFieldsException::class => $this->trans(
                'The %s field is required.',
                [
                    implode(
                        ', ',
                        $e instanceof MissingZoneRequiredFieldsException ? $e->getMissingRequiredFields() : []
                    ),
                ],
                'Admin.Notifications.Error'
            ),
            ZoneNotFoundException::class => $this->trans(
                'This zone does not exist.',
                [],
                'Admin.Notifications.Error'
            ),
            CannotToggleZoneStatusException::class => $this->trans(
                'An error occurred while updating the status.',
                [],
                'Admin.Notifications.Error'
            ),
            DeleteZoneException::class => [
                DeleteZoneException::FAILED_DELETE => $this->trans(
                    'An error occurred while deleting the object.',
                    [],
                    'Admin.Notifications.Error'
                ),
                DeleteZoneException::FAILED_BULK_DELETE => $this->trans(
                    'An error occurred while deleting this selection.',
                    [],
                    'Admin.Notifications.Error'
                ),
            ],
        ];
    }

    /**
     * Collects zone IDs from request.
     *
     * @param Request $request
     *
     * @return array
     */
    private function getBulkZonesFromRequest(Request $request): array
    {
        $zoneIds = $request->request->all('zone_bulk');

        return array_map('intval', $zoneIds);
    }

    /**
     * @return array
     */
    private function getZoneToolbarButtons(): array
    {
        return [
            'add' => [
                'href' => $this->generateUrl('admin_zones_create'),
                'desc' => $this->trans('Add new zone', [], 'Admin.International.Feature'),
                'icon' => 'add_circle_outline',
            ],
        ];
    }
}
