<?php
/**
 * Created by PhpStorm.
 * User: radko
 * Date: 23.7.18
 * Time: 14:54
 */

namespace Seka\Vzdelavanie\Repositories\Eloquent;

use Spatie\QueryBuilder\QueryBuilder;
use Illuminate\Container\Container as App;
use Seka\Vzdelavanie\Repositories\Contracts\RepositoryInterface;
use Seka\Vzdelavanie\Repositories\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundExceptionEloquent;

/**
 * Class Repository
 * @package Seka\Vzdelavanie\Repositories\Eloquent
 */
abstract class Repository implements RepositoryInterface
{
    /**
     * @var \Illuminate\Container\Container
     */
    private $app;

    /**
     * @var \Illuminate\Database\Eloquent\Builder
     */
    protected $model;

    /**
     * @var \Spatie\QueryBuilder\QueryBuilder
     */
    protected $query;

    /**
     * Repository constructor.
     * @param \Illuminate\Container\Container $app
     * @throws \Seka\Vzdelavanie\Repositories\Exceptions\RepositoryException
     */
    public function __construct(App $app)
    {
        $this->app = $app;
    }

    /**
     * @return \Illuminate\Database\Eloquent\Model
     */
    abstract public function model();

    /**
     * @param array $attributes
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     * @throws \Seka\Vzdelavanie\Repositories\Exceptions\RepositoryException
     */
    public function make(array $attributes)
    {
        return $this->query()->newModelInstance($attributes);
    }

    /**
     * @param array $attributes
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     * @throws \Seka\Vzdelavanie\Repositories\Exceptions\RepositoryException
     */
    public function create(array $attributes)
    {
        return $this->query()->create($attributes);
    }

    /**
     * @param array  $attributes
     * @param int    $id
     * @param string $attribute
     * @return int|mixed
     * @throws \Seka\Vzdelavanie\Repositories\Exceptions\RepositoryException
     */
    public function update(array $attributes, int $id, $attribute = 'id')
    {
        return $this->query()->where($attribute, $id)->update($attributes);
    }

    /**
     * @param int $id
     * @return bool|mixed|null
     * @throws \Seka\Vzdelavanie\Repositories\Exceptions\RepositoryException
     */
    public function delete(int $id)
    {
        return $this->query()->find($id)->delete();
    }

    /**
     * @return mixed|\Spatie\QueryBuilder\QueryBuilder
     */
    public function query()
    {
        $this->query = QueryBuilder::for($this->model());

        return $this->query;
    }

    /**
     * @param array $columns
     * @return mixed
     */
    public function all(array $columns = array('*'))
    {
        return $this->query()->get($columns);
    }

    /**
     * @param int   $perPage
     * @param array $columns
     * @return mixed
     */
    public function paginate(int $perPage = 15, array $columns = array('*'))
    {
        return $this->query()->paginate($perPage, $columns, 'strana');
    }

    /**
     * @param int   $id
     * @param array $columns
     * @return mixed
     * @throws \Seka\Vzdelavanie\Repositories\Exceptions\ModelNotFoundException
     */
    public function find(int $id, array $columns = array('*'))
    {
        try {
            return $this->query()->find($id, $columns);
        }
        catch (ModelNotFoundExceptionEloquent $exc) {
            throw new ModelNotFoundException();
        }
    }

    /**
     * @param string $field
     * @param mixed  $value
     * @param array  $columns
     * @return mixed
     */
    public function findBy(string $field, $value, array $columns = array('*')) {
        return $this->query()->where($field, '=', $value)->first($columns);
    }

    /**
     * @param string $field
     * @param        $value
     * @param array  $columns
     * @return $this
     */
    public function where(string $field, $value, array $columns = array('*'))
    {
        $this->query->where($field, $value, $columns);

        return $this;
    }

    /**
     * @param string $fieldName
     * @param string $value
     * @return $this
     */
    public function orderBy(string $fieldName, string $value)
    {
        $this->query->orderBy($fieldName, $value);

        return $this;
    }

    /**
     * @param int|null $value
     * @return $this
     */
    public function limit(int $value = null)
    {
        $this->query->limit($value);

        return $this;
    }

//    /**
//     * @return \Illuminate\Database\Eloquent\Model
//     */
//    public function getModel()
//    {
//        return $this->model;
//    }
//
//    /**
//     * @param string $model
//     * @return $this
//     */
//    public function setModel($model)
//    {
//        $this->model = $model;
//
//        return $this;
//    }

    /**
     * @param array|string $relations
     * @return $this|mixed
     */
    public function with($relations)
    {
        if (is_string($relations)) $relations = func_get_args();

        $this->query->with = $relations;

        return $this;
    }

    /**
     * @param $relations
     * @return $this
     */
    public function withCount($relations)
    {
        if (is_string($relations)) $relations = func_get_args();

        $this->query->withCount($relations);

        return $this;
    }

    /**
     * @return $this
     */
    protected function eagerLoadRelations()
    {
        if(!is_null($this->query->with)) {
            foreach ($this->query->with as $relation) {
                $this->query->with($relation);
            }
        }

        return $this;
    }
}
