<?php
    declare(strict_types=1);

    namespace Icing\Controller;

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

    /**
     * UserGroups Controller
     *
     * @property \Icing\Model\Table\UserGroupsTable $UserGroups
     */
    class UserGroupsController extends AppController
    {

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

            $userGroups = $this->paginate($userGroups);
            $this->set(compact('userGroups'));
            $this->set('_serialize', 'userGroups');

            $blankUserGroup = $this->UserGroups->newEmptyEntity();

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

        public function view($id = null): ?Response
        {
            $userGroup = $this->UserGroups->get(
                $id,
                contain: []
            );

            $this->set('userGroup', $userGroup);
            $this->set('_serialize', 'userGroup');
            return null;
        }

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

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

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

        public function edit($id = null): ?Response
        {
            $userGroup = $this->UserGroups->get(
                $id,
                contain: ['Roles']
            );

            if ($this->request->is(['patch', 'post', 'put'])) {
                $userGroup = $this->UserGroups->patchEntity($userGroup, $this->request->getData());

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

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

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

            $roles = $this->UserGroups->Roles->find('list');
            $this->set(compact('userGroup', 'roles'));
            return null;
        }

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

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


    }
