<?php

namespace App\Controller\Member;

use App\Controller\Member\AppMemberController;
use Cake\Event\Event;
use Cake\Network\Exception\NotFoundException;

class BundlesController extends AppMemberController
{
    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        if (!$this->loggedin_user_plan->bundle) {
            $this->Flash->error(__('You must upgrade your plan so you can use this tool.'));
            return $this->redirect('/member/dashboard');
        }
    }

    public function index()
    {
        $query = $this->Bundles->find()
            ->contain([
                'Users' => function ($q) {
                    return $q
                        ->select(['username']);
                }
            ])
            ->where([
                'user_id' => $this->Auth->user('id'),
            ]);
        $bundles = $this->paginate($query);

        $this->set('bundles', $bundles);
    }

    public function add()
    {
        $bundle = $this->Bundles->newEntity();

        if ($this->request->is('post')) {
            $bundle = $this->Bundles->patchEntity($bundle, $this->request->data);

            $bundle->user_id = $this->Auth->user('id');
            $bundle->user_bundle_id = $this->Bundles->getNewBundleId($this->Auth->user('id'));

            if ($this->Bundles->save($bundle)) {
                $this->loadModel('Links');

                $query = $this->Bundles->Users->query()
                    ->update()
                    ->set(['last_bundle_id' => $bundle->user_bundle_id])
                    ->where([
                        'id' => $this->Auth->user('id')
                    ])
                    ->execute();

                $this->Flash->success(__('Bundle has been added.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Oops! There are mistakes in the form. Please make the correction.'));
        }
        $this->set('bundle', $bundle);
    }

    public function edit($id = null)
    {
        if (!$id) {
            throw new NotFoundException(__('Invalid Bundle'));
        }

        $bundle = $this->Bundles->find()
            ->where([
                'id' => $id,
                'user_id' => $this->Auth->user('id')
            ])
            ->first();

        if (!$bundle) {
            throw new NotFoundException(__('Invalid Bundle'));
        }

        if ($this->request->is(['post', 'put'])) {
            $bundle = $this->Bundles->patchEntity($bundle, $this->request->data);

            if ($this->Bundles->save($bundle)) {
                $this->Flash->success(__('Bundle has been updated.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Oops! There are mistakes in the form. Please make the correction.'));
        }
        $this->set('bundle', $bundle);
    }

    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);

        $bundle = $this->Bundles->find()
            ->where([
                'id' => $id,
                'user_id' => $this->Auth->user('id')
            ])
            ->first();

        if ($this->Bundles->delete($bundle)) {
            $this->loadModel('Links');

            $query = $this->Links->query()
                ->update()
                ->set(['bundle_id' => 0])
                ->where([
                    'bundle_id' => $id,
                    'user_id' => $this->Auth->user('id')
                ])
                ->execute();

            $this->Flash->success(__('The bundle with id: {0} has been deleted.', $bundle->id));
            return $this->redirect(['action' => 'index']);
        }
    }
}
