<?php
declare(strict_types=1);

namespace App\Controller;

/**
 * SlowQueries Controller
 *
 * @property \App\Model\Table\SlowQueriesTable $SlowQueries
 * @method \App\Model\Entity\SlowQuery[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class SlowQueriesController extends AppController
{
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function index($time = null)
    {
        if($time == '1'){
            $slow = $this->SlowQueries->find()->where(['created BETWEEN DATE_SUB(created, INTERVAL 1 MONTH) AND NOW()']);
        }elseif($time == '2'){
            $slow = $this->SlowQueries->find()->where(['created BETWEEN DATE_SUB(created, INTERVAL 1 WEEK) AND NOW()']);
        }elseif($time == '3'){
            $slow = $this->SlowQueries->find()->where(['created BETWEEN CONCAT(CURDATE(), " 00:00:00") AND NOW()']);
        }else{
            $slow = $this->SlowQueries;
        }

        $slowQueries = $this->paginate($slow);

        $this->set(compact('slowQueries'));
    }

    public function getQuery($id = null){
        $this->autoRender =  false;
        $response = ['result' => false, 'mssg' => 'Error en su solicitud'];

        if($this->request->is('post')){
            $slowQuery = $this->SlowQueries->find()->select(['query'])->where(['id' => $id])->first();

            if($slowQuery){
                $response = ['result' => true, 'mssg' => $slowQuery->query];
            }else{
                $response = ['result' => false, 'mssg' => 'La consulta no fue encontrada'];
            }
        }

        return $this->response($response);
    }

    /**
     * View method
     *
     * @param string|null $id Slow Query id.
     * @return \Cake\Http\Response|null|void Renders view
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function __view($id = null)
    {
        $slowQuery = $this->SlowQueries->get($id, [
            'contain' => [],
        ]);

        $this->set(compact('slowQuery'));
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
     */
    public function __add()
    {
        $slowQuery = $this->SlowQueries->newEmptyEntity();
        if ($this->request->is('post')) {
            $slowQuery = $this->SlowQueries->patchEntity($slowQuery, $this->request->getData());
            if ($this->SlowQueries->save($slowQuery)) {
                $this->Flash->success(__('The slow query has been saved.'));

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

    /**
     * Edit method
     *
     * @param string|null $id Slow Query id.
     * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function __edit($id = null)
    {
        $slowQuery = $this->SlowQueries->get($id, [
            'contain' => [],
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $slowQuery = $this->SlowQueries->patchEntity($slowQuery, $this->request->getData());
            if ($this->SlowQueries->save($slowQuery)) {
                $this->Flash->success(__('The slow query has been saved.'));

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

    /**
     * Delete method
     *
     * @param string|null $id Slow Query id.
     * @return \Cake\Http\Response|null|void Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function __delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $slowQuery = $this->SlowQueries->get($id);
        if ($this->SlowQueries->delete($slowQuery)) {
            $this->Flash->success(__('The slow query has been deleted.'));
        } else {
            $this->Flash->error(__('The slow query could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}
