<?php

namespace App\Models\Pouzivatel;

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

/**
 * 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 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 [];
    }
}
