<?php

namespace App\Modules\Stock;

use Exception;

class StockObserver
{
    public function created($model)
    {
        // $this->addToElasticSearch($model);
        $this->updateRequestedDate($model);
    }

    public function updating($model)
    {
        $this->keepMinimunQuantityToOrder($model);
    }

    public function updated($model)
    {
        $this->updateRequestedDate($model);
        // $this->updateInElasticSearch($model);
    }

    public function deleted($model)
    {
        $this->removeFromElasticSearch($model);
    }

    /**
     * ------------------------------------------------
     * PROTECTED FUNCTIONS BELOW
     * ------------------------------------------------.
     */

    /**
     *	Creates a Transaction for this purchased item.
     *
     * @return void
     */
    public function updateRequestedDate($model)
    {
        if ($model->quantity_to_order > 0 && $model->requested_date == null) {
            $model->requested_date = date('Y-m-d H:i:s');
            if (!$model->save()) {
                throw new Exception('Error updating the requested date in the stock product.', 1);
            }
        } elseif ($model->quantity_to_order == 0 && $model->requested_date != null) {
            $model->requested_date = null;
            if (!$model->save()) {
                throw new Exception(json_encode($model->erros->all()), 1);
            }
        }
    }

    /**
     *	When modifying a stock item, do not let quantity_to_order be less than the quantity required from parts and orders.
     *
     * @return void
     */
    public function keepMinimunQuantityToOrder($model)
    {
        if (!$model->unlimited && ($model->isDirty('quantity_to_order') || $model->isDirty('ordered_quantity'))) {
            $quantity_required = $model->quantity_required();

            if ($quantity_required > $model->quantity_to_order + $model->ordered_quantity + $model->quantity) {
                $model->quantity_to_order = $quantity_required - $model->ordered_quantity - $model->quantity;
            }
        }
    }

    /**
     *	Add indexes to elasticsearch for this model name and model_number.
     *
     * @param  App\Modules\Stock\Stock 	$model
     *
     * @return void
     */
    public function addToElasticSearch($model)
    {
        // $elasticsearch = new \Elasticsearch\Client();
        // $elasticsearch->index([
        //     'index' => 'stock',
        //     'type'  => 'item',
        //     'id'    => $model->id,
        //     'body'  => [
        //         'name'            => $model->name,
        //         'model_number'    => $model->model_number,
        //     ],
        // ]);
    }

    /**
     *	Remove the indexes entries in elasticsearch for this model.
     *
     * @param  App\Modules\Stock\Stock 	$model
     *
     * @return void
     */
    public function updateInElasticSearch($model)
    {
        // if ($model->isDirty('name') || $model->isDirty('model_number')) {
        //     $elasticsearch = new \Elasticsearch\Client();
        //     $elasticsearch->delete(['index' => 'stock', 'type' => 'item', 'id' => $model->id]);
        //     $elasticsearch->index([
        //         'index' => 'stock',
        //         'type'  => 'item',
        //         'id'    => $model->id,
        //         'body'  => [
        //             'name'            => $model->name,
        //             'model_number'    => $model->model_number,
        //         ],
        //     ]);
        // }
    }

    /**
     *	Remove the indexes entries in elasticsearch for this model.
     *
     * @param  App\Modules\Stock\Stock 	$model
     *
     * @return void
     */
    public function removeFromElasticSearch($model)
    {
        // $elasticsearch = new \Elasticsearch\Client();
        // $elasticsearch->delete(['index' => 'stock', 'type' => 'item', 'id' => $model->id]);
    }
}
