<?php

declare(strict_types=1);

namespace Seka\Terminy\Models\MiestoKonania;

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

/**
 * 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\Terminy\Models\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);
    }
}
