<?php

namespace Seka\Vzdelavanie\Models\Kniznica;

use Kalnoy\Nestedset\NodeTrait;
use Core\Http\Traits\Hashidable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Seka\Vzdelavanie\Enums\ModelsTypeEnum;
use Seka\Vzdelavanie\Models\Popularita\Popularita;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Seka\Vzdelavanie\Models\Pouzivatel\Kniznica\Oblubene;

/**
 * Class Sekcia
 * @package Seka\Vzdelavanie\Models\Kniznica
 */
class Sekcia extends Model
{
    use NodeTrait;
    use Hashidable;

    /**
     * @var string
     */
    protected $table = 'vzdelavanie_kniznica_sekcie';

    /**
     * @var array
     */
    protected $fillable = [
        'nazov',
        'obsah',
        'parent_id',
        'ucebnica_id',
    ];

    /**
     * @var string[]
     */
    protected $appends = [
        'url',
        'hash_id',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function ucebnica()
    {
        return $this->belongsTo(Ucebnica::class, 'ucebnica_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function oblubene(): MorphMany
    {
        return $this->morphMany(Oblubene::class, 'oblubene')
            ->where('pouzivatel_id', Auth::id())
            ->limit(1);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function popularita(): MorphMany
    {
        return $this->morphMany(Popularita::class, 'popularita');
    }

    /**
     * @return string
     */
    public static function type(): string
    {
        return ModelsTypeEnum::MODEL_TYPE_KNIZNICA_SEKCIA()->value;
    }

    /**
     * @return array
     */
    protected function getScopeAttributes(): array
    {
        return [
            'ucebnica_id',
        ];
    }

    /**
     * @param       $query
     * @param mixed ...$columns
     *
     * @return mixed
     */
    public function scopeExclude($query, ...$columns)
    {
        return $query->select(
            array_diff(
                $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable()),
                $columns
            )
        );
    }

    /**
     * @return string|null
     */
    public function getUrlAttribute()
    {
        if (! $this->getKey()) return null;

        if (Auth::user()->can('vzdelavanie')) {
            return route(
                'vzdelavanie.sprava.kniznica.kategoria.ucebnice.sekcia.edit',
                [
                    $this->ucebnica->kategoria,
                    $this->ucebnica,
                    $this,
                ]
            );
        }

        return route(
            'vzdelavanie.kniznica.ucebnica.sekcia.show',
            [
                $this->ucebnica,
                $this,
            ]
        );
    }

    /**
     * @param string      $childType
     * @param mixed       $value
     * @param string|null $field
     *
     * @return \Illuminate\Database\Eloquent\Model|void|null
     */
    public function resolveChildRouteBinding($childType, $value, $field)
    {
        // TODO: Implement resolveChildRouteBinding() method.
    }
}
