<?php
/**
 * 2007-2023 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author    PrestaShop SA <contact@prestashop.com>
 *  @copyright 2007-2023 PrestaShop SA
 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */
declare(strict_types=1);

namespace PrestaShop\Module\AgileMultisellerPro\Controller\Admin;

use PrestaShop\Module\AgileMultisellerPro\Grid\SellerMembershipsTypes\AgileDefinitionFactory;
use PrestaShop\Module\AgileMultisellerPro\Grid\SellerMembershipsTypes\AgileFilters;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use PrestaShopBundle\Security\Annotation\ModuleActivated;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * Class AdminSellerMembershipsTypes.
 *
 * @ModuleActivated(moduleName="agilemultisellerpro", redirectRoute="admin_module_manage")
 */
class SellerMembershipsTypes extends FrameworkBundleAdminController
{
    /**
     * List membership types
     *
     * @AdminSecurity("is_granted('read', request.get('_legacy_controller'))", message="Access denied.")
     *
     * @param AgileFilters $filters
     *
     * @return Response
     */
    public function index(AgileFilters $filters): Response
    {
        $dataGridFactory = $this->get('prestashop.module.agilemultisellerpro.grid.factory.sellermembershipstypes');
        $dataGrid = $dataGridFactory->getGrid($filters);

        return $this->render(
            '@Modules/agilemultisellerpro/views/templates/admin/form/grid_form.html.twig',
            [
                'enableSidebar' => true,
                'layoutTitle' => $this->trans('Seller membership types', 'Modules.Agilemultisellerpro.Admin'),
                'layoutHeaderToolbarBtn' => [],
                'dataGrid' => $this->presentGrid($dataGrid),
            ]
        );
    }

    /**
     * Provides filters functionality.
     *
     * @param Request $request
     *
     * @return RedirectResponse
     */
    public function searchAction(Request $request)
    {
        /** @var ResponseBuilder $responseBuilder */
        $responseBuilder = $this->get('prestashop.bundle.grid.response_builder');

        return $responseBuilder->buildSearchResponse(
            $this->get('prestashop.module.agilemultisellerpro.grid.sellermembershipstypes.definitionfactory'),
            $request,
            AgileDefinitionFactory::GRID_ID,
            'admin_agile_seller_memberships_types'
        );
    }

    /**
     * Edit Membership
     *
     * @param Request $request
     * @param int $sellerMembershipTypeId
     *
     * @return Response
     */
    public function editAction(Request $request, $sellerMembershipTypeId)
    {
        $isError = false;
        $sellerMembershipTypeFormBuilder = $this->get('prestashop.module.agilemultisellerpro.form.sellermembershipstype.form_builder');
        $sellerMembershipType = $sellerMembershipTypeFormBuilder->getFormFor((int) $sellerMembershipTypeId);
        $sellerMembershipType->handleRequest($request);

        try {
            $sellerMembershipTypeFormHandler = $this->get('prestashop.module.agilemultisellerpro.form.sellermembershipstype.form_handler');
            $result = $sellerMembershipTypeFormHandler->handleFor((int) $sellerMembershipTypeId, $sellerMembershipType);
        } catch (\Exception $e) {
            $this->addFlash(
                'error',
                $e->getMessage()
            );
            $isError = true;
        }

        if (false === $isError && $result->isSubmitted() && $result->isValid()) {
            $this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));

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

        return $this->render(
            '@Modules/agilemultisellerpro/views/templates/admin/form/edit.html.twig',
            [
                'formTitle' => $this->trans('Seller membership types', 'Modules.Agilemultisellerpro.Admin'),
                'formPathReturn' => $this->generateUrl('admin_agile_seller_memberships_types'),
                'dataForm' => $sellerMembershipType->createView(),
            ]
        );
    }
}
