<?php

namespace App\Models\Goodnight;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Goodnight\RoomModel
 *
 * @property int $id
 * @property string $name
 * @property int $activity_id
 * @property int $number
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @property int $interacted_at
 * @property-read \App\Models\Goodnight\ActivityModel $activity
 * @property-read \App\Models\Goodnight\RoomMessageModel $lastMessage
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Goodnight\RoomMemberModel[] $members
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel whereActivityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel whereInteractedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel whereNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Goodnight\RoomModel whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class RoomModel extends Model
{
    //
    protected $table = 'kdgx_goodnight_rooms';

    protected $dateFormat = "U";

    protected $fillable = ['name', 'number', 'activity_id', 'interacted_at'];


    public function members()
    {
        return $this->hasMany(RoomMemberModel::class, 'room_id', 'id');
    }

    public function lastMessage()
    {
        return $this->hasOne(RoomMessageModel::class, 'room_id', 'id')->orderBy('created_at', 'desc');
    }


    public function activity()
    {
        return $this->belongsTo(ActivityModel::class);
    }
}
