<?php

namespace App\Models\Pouzivatel;

use App\Models\Hodnotenie\HodnotenieModel;
use App\Models\Pracovisko\Pracovisko;
use Elceka\Laravel\HashId\Http\Traits\Hashidable;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User;
use Illuminate\Notifications\Notifiable;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
use Spatie\Permission\Traits\HasRoles;

/**
 * Class Pouzivatel
 *
 * @package App\Models\Pouzivatel
 * @property int $id
 * @property int $pracovisko_id
 * @property string $meno
 * @property string $priezvisko
 * @property ?string $telefon
 * @property ?string $email
 * @property string $cele_meno
 * @property string $cele_meno_prve_priezvisko
 * @property PouzivatelStavEnum $stav
 * @property string $login
 * @property string $notification_token
 * @property string $password
 * @property \DateTimeInterface $datum_posledneho_prihlasenia
 */
final class Pouzivatel extends User implements Authenticatable, JWTSubject
{
    use HasRoles;
    use Notifiable;
    use Hashidable;

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

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

    /**
     * @var array
     */
    protected $guarded = [
        'password',
    ];

    /**
     * @var string[]
     */
    protected $hidden = [
        'password',
        'notification_token',
    ];

    /**
     * @var string[]
     */
    protected $dates = [
        'datum_posledneho_prihlasenia',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'stav' => PouzivatelStavEnum::class,
    ];

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

    /**
     * @return HasMany
     */
    public function hodnotenia(): HasMany
    {
        return $this->hasMany(HodnotenieModel::class, 'hodnotil_id', 'id');
    }

    /**
     * @return string
     */
    public function getCeleMenoAttribute(): string
    {
        return sprintf('%s %s', $this->getAttribute('meno'), $this->getAttribute('priezvisko'));
    }

    /**
     * @return string
     */
    public function getCeleMenoPrvePriezviskoAttribute(): string
    {
        return sprintf('%s %s', $this->getAttribute('priezvisko'), $this->getAttribute('meno'));
    }

    /**
     * @param \Illuminate\Database\Eloquent\Builder $query
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeAktivny(Builder $query): Builder
    {
        return $query->where('stav', PouzivatelStavEnum::AKTIVNY()->value);
    }

    /**
     * @return bool
     */
    public function jeBlokovany(): bool
    {
        return (bool)$this->getAttribute('stav')->equals(PouzivatelStavEnum::BLOKOVANY());
    }

    /**
     * @return bool
     */
    public function jeAktivny(): bool
    {
        return (bool)$this->getAttribute('stav')->equals(PouzivatelStavEnum::AKTIVNY());
    }

    /**
     * Specifies the user's FCM token
     *
     * @return string|null
     */
    public function routeNotificationForFcm(): ?string
    {
        return $this->notification_token;
    }

    /**
     * Route notifications for the mail channel.
     *
     * @param \Illuminate\Notifications\Notification $notification
     *
     * @return array
     */
    public function routeNotificationForMail($notification)
    {
        return [$this->email => $this->cele_meno];
    }

    /**
     * @return int
     */
    public function getJWTIdentifier(): int
    {
        return $this->getKey();
    }

    /**
     * @return array
     */
    public function getJWTCustomClaims(): array
    {
        return [];
    }
}
