<?php

namespace App\Models_v2;

use App\Jobs\SendNotificationUserNotReply;
use App\Jobs\SendNotificationUserNotRequest;
use App\Jobs\SendWhenOtherInteract;
use App\Models\Hostel;
use App\Models\Room;
use App\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Conversation extends Model
{
    //
    protected $table = 'conversation_find_hostels';

    const INACTIVE = 1;
    const ARCHIVED = 2;

    protected $fillable = [
        'name',
        'image',
        'channel',
        'is_visible',
        'status',
        'find_session_id',
        'hostel_id',
        'date_message',
        'request_confirm_id',
        'members',
        'is_find',
        'is_manual',
        'room_id',
        'allow_tenant_reply'
    ];

    protected $dates = [
        'created_at',
        'updated_at'
    ];

    public static function boot()
    {
        parent::boot();

        static::created(function ($item) {
            //dispatch(new SendWhenOtherInteract($item->id));
            $jobNotificationNotReply = (new SendNotificationUserNotReply($item->id))->delay(Carbon::now()->addMinutes(30));
            dispatch($jobNotificationNotReply);
            if (empty($item->members)) {
                $users = $item->users->pluck('id')->toArray();
                sort($users);
                $item->members = json_encode($users);
                $item->save();
            }
        });
    }

    public function findSession()
    {
        return $this->belongsTo(FindSession::class, 'find_session_id', 'id');
    }

    public function hostel()
    {
        return $this->belongsTo(Hostel::class, 'hostel_id', 'id');
    }

    public function room()
    {
        return $this->belongsTo(Room::class, 'room_id', 'id');
    }

    public function users()
    {
        return $this->belongsToMany(User::class, 'user_conversation_find_hostels', 'conversation_id', 'user_id')
            ->withTimestamps('created_at', 'updated_at');
    }

    public function hostels()
    {
        return $this->belongsToMany(Hostel::class, 'conversation_hostels', 'conversation_id', 'hostel_id');
    }

    public function messages()
    {
        return $this->hasMany(Message::class, 'conversation_id', 'id');
    }

    public function getOtherUserAttribute($user)
    {
        return $this->users()
            ->where('users.id', '<>', $user->id)
            ->first();
    }


    public function unreadMessages($userId)
    {
        return $this->hasManyThrough(
            MessageRead::class,
            Message::class,
            'conversation_id',
            'message_id',
            'id',
            'id'
        )
            ->where('message_find_hostel_read_at.user_id', $userId)
            ->whereNull('message_find_hostel_read_at.read_at');
    }

    public function readMessage()
    {
        return $this->hasManyThrough(
            MessageRead::class,
            Message::class,
            'conversation_id',
            'message_id',
            'id',
            'id'
        );
    }
}
