<?php

namespace App\Models_v2;

use App\Models\Hostel;
use App\User;
use Illuminate\Database\Eloquent\Model;

class Message extends Model {
	//text, image, system, images, previewURL, location, audio, video,hostels
	protected $table = 'message_find_hostels';

	protected $fillable = [
		'from',
		'type',
		'ui_id',
		'conversation_id',
		'lat',
		'lng',
		'preview',
		'content',
		'request_confirm_id'
	];

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

	protected $casts = [
		'preview' => 'array'
	];

	public function attachments()
	{
		return $this->hasMany(Attachment::class, 'message_id', 'id');
	}

	public function conversation()
	{
		return $this->belongsTo(Conversation::class, 'conversation_id', 'id');
	}

	public function sender()
	{
		return $this->belongsTo(User::class, 'from', 'id');
	}

	public function requestConfirm()
	{
		return $this->belongsTo(RequestConfirm::class);
	}

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

	public function getDisplayTextAttribute() {
		$type    = $this->attributes['type'];
		$content = null;
		if(isset($this->attributes['content'])) {
			$content = $this->attributes['content'];
		}

		$message = null;
		switch ( $type ) {
			case 'image':
				$message = 'Tin nhắn ảnh';
				break;
			case 'images':
				$message = 'Tin nhắn ảnh';
				break;

			case 'location':
				$message = 'Tin nhắn toạ độ';
				break;
			case 'video':
				$message = 'Tin nhắn video';
				break;
			case 'audio':
				$message = 'Tin nhắn âm thanh';
				break;
			case 'transaction':
				$message = 'Tin nhắn xác nhận';
				break;

			case 'hostel':
				$message = 'Tin nhắn đính kèm';
				break;
			case 'hostels':
				$message = 'Tin nhắn đính kèm';
				break;
			default:
				$message = $content;
				break;
		}

		return $message;
	}
}
