<?php
declare(strict_types=1);

namespace CakeProducts\Controller;

use Cake\Log\Log;
use CakeProducts\Controller\AppController;

/**
 * ExternalProductCatalogs Controller
 *
 * @property \CakeProducts\Model\Table\ExternalProductCatalogsTable $ExternalProductCatalogs
 */
class ExternalProductCatalogsController extends AppController
{
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function index()
    {
        $query = $this->ExternalProductCatalogs->find()
            ->contain(['ProductCatalogs']);
        $externalProductCatalogs = $this->paginate($query);

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

    /**
     * View method
     *
     * @param string|null $id External Product Catalog id.
     * @return \Cake\Http\Response|null|void Renders view
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $externalProductCatalog = $this->ExternalProductCatalogs->get($id, contain: ['ProductCatalogs']);
        $this->set(compact('externalProductCatalog'));
    }

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

                return $this->redirect(['action' => 'index']);
            }
            Log::debug(print_r('$externalProductCatalog->getErrors() next - failed /add', true));
            Log::debug(print_r($externalProductCatalog->getErrors(), true));
            $this->Flash->error(__('The external product catalog could not be saved. Please, try again.'));
        }
        $productCatalogs = $this->ExternalProductCatalogs->ProductCatalogs->find('list', limit: 200)->all();
        $this->set(compact('externalProductCatalog', 'productCatalogs'));
    }

    /**
     * Edit method
     *
     * @param string|null $id External Product Catalog 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)
    {
        $externalProductCatalog = $this->ExternalProductCatalogs->get($id, contain: []);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $externalProductCatalog = $this->ExternalProductCatalogs->patchEntity($externalProductCatalog, $this->request->getData());
            if ($this->ExternalProductCatalogs->save($externalProductCatalog)) {
                $this->Flash->success(__('The external product catalog has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            Log::debug(print_r('$externalProductCatalog->getErrors() next - failed /edit', true));
            Log::debug(print_r($externalProductCatalog->getErrors(), true));
            $this->Flash->error(__('The external product catalog could not be saved. Please, try again.'));
        }
        $productCatalogs = $this->ExternalProductCatalogs->ProductCatalogs->find('list', limit: 200)->all();
        $this->set(compact('externalProductCatalog', 'productCatalogs'));
    }

    /**
     * Delete method
     *
     * @param string|null $id External Product Catalog id.
     * @return \Cake\Http\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $externalProductCatalog = $this->ExternalProductCatalogs->get($id);
        if ($this->ExternalProductCatalogs->delete($externalProductCatalog)) {
            $this->Flash->success(__('The external product catalog has been deleted.'));
        } else {
            $this->Flash->error(__('The external product catalog could not be deleted. Please, try again.'));
        }

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