<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Terminy\Entities\MoznostiUdalosti;

use Carbon\Carbon;
use Elceka\Laravel\HashId\Hasher;
use Elceka\Laravel\HashId\Http\Traits\Hashidable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

/**
 * Class MoznostUdalosti
 *
 * @property int                                                                           $id
 * @property int                                                                           $vytvoril_id
 * @property string                                                                        $nazov
 * @property string                                                                        $kod
 * @property \Seka\Portal\Modul\Terminy\Entities\MoznostiUdalosti\StavMoznostiUdalostiEnum $stav
 * @property \Carbon\Carbon                                                                $created_at
 * @property \Carbon\Carbon                                                                $updated_at
 * @property \Carbon\Carbon                                                                $deleted_at
 *
 * @package Seka\Portal\Modul\Terminy\Entities\MoznostiUdalosti
 */
class MoznostUdalosti extends Model
{
    use Hashidable;
    use SoftDeletes;

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

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

    /**
     * @var string[]
     */
    protected $fillable = [
        'nazov',
        'kod',
        'stav',
    ];

    /**
     * @return void
     */
    protected static function boot(): void
    {
        parent::boot();

        static::creating(function (MoznostUdalosti $moznostiUdalosti) {
            $moznostiUdalosti->kod = sprintf(
                '%s_%s_%s',
                Str::slug($moznostiUdalosti->nazov),
                Hasher::encode(Carbon::now()->timestamp),
                mt_rand(1, 1024),
            );
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function vytvoril(): BelongsTo
    {
        return $this->belongsTo('\App\Models\Pouzivatel\Pouzivatel', 'vytvoril_id', 'id');
    }
}
