<?php
    declare(strict_types=1);

    namespace Icing\Controller;

    use Cake\Http\Response;
    use Cake\ORM\Exception\PersistenceFailedException;

    /**
     * Permissions Controller
     *
     * @property \Icing\Model\Table\PermissionsTable $Permissions
     */
    class PermissionsController extends AppController
    {

        public function index(): ?Response
        {
            $permissions = $this->Permissions->find('viaQuery', params: $this->getRequest()->getQuery());

            $permissions = $this->paginate($permissions);
            $this->set(compact('permissions'));
            $this->viewBuilder()->setOption('serialize', 'permissions');

            $newPermission = $this->Permissions->newEmptyEntity();

            $this->set(compact('newPermission'));
            return null;
        }

        public function view(string $id = null): ?Response
        {
            $permission = $this->Permissions->get($id);

            $this->set('permission', $permission);
            $this->viewBuilder()->setOption('serialize', 'permission');
            return null;
        }

        public function add(): ?Response
        {
            $permission = $this->Permissions->newEntity($this->request->getQuery(), ['validate' => false]);
            if ($this->request->is('post')) {
                $permission = $this->Permissions->patchEntity($permission, $this->request->getData());

                try {
                    $this->Permissions->saveOrFail($permission);
                    $this->Flash->success(__('The permission has been saved.'));
                    return $this->redirect(['action' => 'view', $permission->id]);
                } catch (PersistenceFailedException) {
                    $this->Flash->error(__('The permission could not be saved. Please, try again.'));
                    $this->Flash->entityError($permission);
                }

            }
            $this->set(compact('permission'));
            return null;
        }

        public function edit($id = null): ?Response
        {
            $permission = $this->Permissions->get(
                $id,
                ['contain' => []]
            );
            if ($this->request->is(['patch', 'post', 'put'])) {
                $permission = $this->Permissions->patchEntity($permission, $this->request->getData());

                if ($this->request->is('json')) {
                    $this->Permissions->saveOrFail($permission);
                    $this->set(compact('permission'));
                    $this->set('_serialize', 'permission');
                    return null;
                }
                if ($this->Permissions->save($permission)) {
                    $this->Flash->success(__('The permission has been saved.'));

                    if ($this->request->getQuery('_r')) {
                        return $this->redirect($this->request->getQuery('_r'));
                    }

                    return $this->redirect(['action' => 'index']);
                }
                $this->Flash->error(__('The permission could not be saved. Please, try again.'));
            }
            $this->set(compact('permission'));
            return null;
        }

        public function delete($id = null): ?Response
        {
            $this->request->allowMethod(['post', 'delete']);
            $permission = $this->Permissions->get($id);
            if ($this->Permissions->delete($permission)) {
                $this->Flash->success(__('The permission has been deleted.'));
            } else {
                $this->Flash->error(__('The permission could not be deleted. Please, try again.'));
                $this->Flash->entityError($permission);
            }

            if ($this->request->getQuery('_r')) {
                return $this->redirect($this->request->getQuery('_r'));
            }
            return $this->redirect(['action' => 'index']);
        }
    }
