<?php

namespace Seka\Vzdelavanie\Models\Kniznica;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Seka\Vzdelavanie\Models\Pouzivatel\Kniznica\Oblubene;
use Seka\Vzdelavanie\Models\Pouzivatel\Kniznica\OblubeneModelTypeEnum;

/**
 * Class Ucebnica
 * @package Seka\Vzdelavanie\Models\Kniznica
 */
class Ucebnica extends Model
{
    /**
     * @var string
     */
    protected $table = 'vzdelavanie_kniznica_ucebnice';

    /**
     * @var array
     */
    protected $fillable = [
        'autor',
        'nazov',
        'popis',
        'verejne',
        'kategoria_id',
        'obrazok',
    ];

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

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

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function sekcie(): HasMany
    {
        return $this->hasMany(Sekcia::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 string
     */
    public static function type(): string
    {
        return OblubeneModelTypeEnum::MODEL_TYPE_UCEBNICA();
    }

    /**
     * @return string
     */
    public function getUrlAttribute(): string
    {
        return URL::route(
            'vzdelavanie.kniznica.ucebnica.show',
            [
                'id' => $this->getKey(),
            ]
        );
    }
}
