<?php

declare(strict_types=1);

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

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Elceka\Laravel\HashId\Http\Traits\Hashidable;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Seka\Portal\Modul\Terminy\Entities\Udalost\Udalost;

/**
 * Class MiestoKonania
 *
 * @property int $id
 * @property int $vytvoril_id
 * @property string $nazov
 * @property string $ulica
 * @property string $obec
 * @property string $psc
 * @property string $krajina
 * @property string $adresa
 * @property float $latitude
 * @property float $longitude
 * @property \DateTime $created_at
 * @property \DateTime $updated_at
 * @property \DateTime $deleted_at
 *
 * @package Seka\Portal\Modul\Terminy\Entities\MiestoKonania
 */
final class MiestoKonania extends Model
{
    use Hashidable;
    use SoftDeletes;

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

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

    /**
     * @var string[]
     */
    protected $casts = [
        'latitude' => 'float',
        'longitude' => 'float',
    ];

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

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

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function udalosti(): HasMany
    {
        return $this->hasMany(Udalost::class, 'miesto_konania_id', 'id');
    }

    /**
     * @return string
     */
    public function getAdresaAttribute(): string
    {
        return sprintf(
            '%s, %s, %s, %s, %s',
            $this->nazov,
            $this->ulica,
            $this->obec,
            $this->psc,
            $this->krajina
        );
    }

    /**
     * Je nastavena geograficka poloha miesta?
     *
     * @return bool
     */
    public function obsahujeGeografickuPolohu(): bool
    {
        return !is_null($this->latitude) && !is_null($this->longitude);
    }
}
