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

namespace Seka\Vzdelavanie\Repositories\Eloquent;

use Illuminate\Support\Arr;
use Seka\Vzdelavanie\Models\Kniznica\Ucebnica;
use Seka\Vzdelavanie\Repositories\Contracts\UcebnicaRepositoryInterface;

/**
 * Class UcebnicaRepository
 * @package Seka\Vzdelavanie\Repositories\Eloquent
 */
class UcebnicaRepository extends Repository implements UcebnicaRepositoryInterface
{
    /**
     * @return string
     */
    public function model()
    {
        return Ucebnica::class;
    }

    /**
     * @param array $attributes
     * @return \Illuminate\Database\Eloquent\Model|mixed
     * @throws \Throwable
     */
    public function create(array $attributes)
    {
        if (Arr::has($attributes, ['verejne'])) {
            $attributes['verejne'] = (bool) $attributes['verejne'];
        } else {
            $attributes['verejne'] = false;
        }

        $attributes['kategoria_id'] = $attributes['kategoria'];

        return parent::create($attributes);
    }

    /**
     * @param array $columns
     * @return mixed|\Spatie\QueryBuilder\QueryBuilder
     */
    public function all(array $columns = ['*'])
    {
        return $this->query()
            ->allowedIncludes('kategoria')
            ->allowedFilters('id', 'kategoria_id')
            ->allowedSorts('updated_at');
    }
}
