<?php

namespace App\Controller\CRUD;

use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

class CRUDController extends Controller
{

    // /**
    //  * @inheritdoc
    //  */
    // public function exportAction(Request $request)
    // {
    //     $this->admin->checkAccess('export');

    //     $format = $request->get('format');

    //     // // NEXT_MAJOR: remove the check
    //     // if (!$this->has('sonata.admin.admin_exporter')) {
    //     //     @trigger_error(
    //     //         'Not registering the exporter bundle is deprecated since version 3.14. You must register it to be able to use the export action in 4.0.',
    //     //         \E_USER_DEPRECATED
    //     //     );
    //     //     $allowedExportFormats = (array) $this->admin->getExportFormats();

    //     //     $class = (string) $this->admin->getClass();
    //     //     $filename = sprintf(
    //     //         'export_%s_%s.%s',
    //     //         strtolower((string) substr($class, strripos($class, '\\') + 1)),
    //     //         date('Y_m_d_H_i_s', strtotime('now')),
    //     //         $format
    //     //     );
    //     //     $exporter = $this->get('sonata.admin.exporter');
    //     // } else {
    //         $adminExporter = $this->get('sonata.admin.admin_exporter');
    //         $allowedExportFormats = $adminExporter->getAvailableFormats($this->admin);
    //         $filename = $adminExporter->getExportFilename($this->admin, $format);
    //         $exporter = $this->get('sonata.exporter.exporter');
    //     // }

    //     if (!\in_array($format, $allowedExportFormats, true)) {
    //         throw new \RuntimeException(sprintf(
    //             'Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`',
    //             $format,
    //             $this->admin->getClass(),
    //             implode(', ', $allowedExportFormats)
    //         ));
    //     }

    //     return $exporter->getResponse(
    //         $format,
    //         $filename,
    //         $this->admin->getDataSourceIterator()
    //     );
    // }

    public function deleteAction($id)
    {
        $request = $this->getRequest();
        $id      = $request->get($this->admin->getIdParameter());
        $object  = $this->admin->getObject($id);

        if (!$object) {
            throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
        }

        $currentUserId = $this->getUser()->getId(); // ID of the current user
        if ($currentUserId == $id) {
            $this->addFlash(
                'sonata_flash_error',
                'Vous ne pouvez pas supprimer votre compte !'
            );

            return $this->redirectTo($object);
        }
        if ($object->hasRole('ROLE_SUPER_ADMIN') || $object->hasRole('ROLE_ADMIN_SIEGE')) {
            $this->addFlash(
                'sonata_flash_error',
                'Vous ne pouvez pas supprimer le compte admin !'
            );

            return $this->redirectTo($object);
        }

        return parent::deleteAction($id);
    }

    public function batchActionDelete(ProxyQueryInterface $query)
    {
        $request       = $this->getRequest();
        $currentUserId = $this->getUser()->getId(); // ID of the current user
        $selectedUsers = $query->execute();

        foreach ($selectedUsers as $selectedUser) {
            if ($selectedUser->getId() == $currentUserId) {
                $this->addFlash(
                    'sonata_flash_error',
                    'Vous ne pouvez pas supprimer votre compte !'
                );

                return new RedirectResponse(
                    $this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters()))
                );
            }
            if ($selectedUser->hasRole('ROLE_SUPER_ADMIN') || $selectedUser->hasRole('ROLE_ADMIN_SIEGE')) {
                $this->addFlash(
                    'sonata_flash_error',
                    'Vous ne pouvez pas supprimer le compte admin !'
                );

                return new RedirectResponse(
                    $this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters()))
                );
            }
        }

        return parent::batchActionDelete($query);
    }
}
