<?php

declare(strict_types=1);

namespace Seka\Vzdelavanie\Models\Testovanie;

use Elceka\Laravel\HashId\Http\Traits\Hashidable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Seka\Vzdelavanie\Enums\ModelStatusEnum;

/**
 * Class Odpoved
 *
 * @package Seka\Vzdelavanie\Models\Testovanie
 */
class Odpoved extends Model
{
    use Hashidable;
    use HasFactory;

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

    /**
     * @var string[]
     */
    protected $fillable = [
        'id',
        'stav',
        'znenie',
        'hodnotenie',
    ];

    /**
     * @var string[]
     */
    protected $casts = [
        'stav' => ModelStatusEnum::class,
        'hodnotenie' => HodnotenieOdpovedeEnum::class,
    ];

    /**
     * @param \Illuminate\Database\Eloquent\Builder $query
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopePublikovana(Builder $query): Builder
    {
        return $query->where('stav', ModelStatusEnum::PUBLIKOVANE);
    }

    /**
     * @param \Illuminate\Database\Eloquent\Builder $query
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeSpravna(Builder $query): Builder
    {
        return $query->where('hodnotenie', HodnotenieOdpovedeEnum::SPRAVNA);
    }

    /**
     * @param \Illuminate\Database\Eloquent\Builder $query
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeNespravna(Builder $query): Builder
    {
        return $query->where('hodnotenie', HodnotenieOdpovedeEnum::NESPRAVNA);
    }

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

    /**
     * @return bool
     */
    public function jePublikovana(): bool
    {
        return ModelStatusEnum::PUBLIKOVANE === $this->getAttribute('stav');
    }

    /**
     * @return bool
     */
    public function jeSpravna(): bool
    {
        return HodnotenieOdpovedeEnum::SPRAVNA === $this->getAttribute('hodnotenie');
    }

    /**
     * @return bool
     */
    public function jeNespravna(): bool
    {
        return HodnotenieOdpovedeEnum::NESPRAVNA === $this->getAttribute('hodnotenie');
    }
}
