<?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\Factories\HasFactory;
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
 *
 * @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 HasFactory;
    use Hashidable;
    use HasRoles;
    use Notifiable;

    /**
     * @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[]
     */
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'datum_posledneho_prihlasenia' => 'datetime',
        'stav' => PouzivatelStavEnum::class,
    ];

    public function pracovisko(): BelongsTo
    {
        return $this->belongsTo(Pracovisko::class, 'pracovisko_id', 'id');
    }

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

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

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

    public function scopeAktivny(Builder $query): Builder
    {
        return $query->where('stav', PouzivatelStavEnum::AKTIVNY()->value);
    }

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

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

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

    /**
     * Route notifications for the mail channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return null|string|array
     */
    public function routeNotificationForMail($notification)
    {
        if (is_null($this->email) || empty(trim($this->email))) {
            return $this->email;
        }

        return [$this->email => $this->cele_meno];
    }

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

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