<?php

declare(strict_types=1);

namespace App\Models\Pracovisko;

use App\Models\Pouzivatel\Pouzivatel;
use Elceka\Laravel\HashId\Http\Traits\Hashidable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;

/**
 * Class Pracovisko
 *
 * @property $id
 * @property $kod
 * @property $nazov
 * @property $email
 */
final class Pracovisko extends Model
{
    use HasFactory;
    use Hashidable;
    use Notifiable;

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

    /**
     * @var string[]
     */
    protected $guarded = [];

    protected static function boot(): void
    {
        parent::boot();

        self::creating(function (Pracovisko $pracovisko) {
            $pracovisko->email = sprintf('%s@sekait.sk', $pracovisko->kod);
        });
    }

    public function pouzivatelia(): HasMany
    {
        return $this->hasMany(Pouzivatel::class, 'pracovisko_id', 'id');
    }

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

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