﻿<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\OrganisationScope;
use Illuminate\Support\Facades\Auth;

class MessageThread extends Model
{
protected $guarded  = [];
    use HasFactory;
    protected $table    = 'message_threads';
    protected $fillable = [
        'code',
        'contact_one',
        'contact_two',
        'created_at',
        'updated_at',
        'organisation_id'
    ];

    public function user()
    {
        $user = $this->belongsTo(User::class, 'contact_one', 'id');

        if ($user->value('id') != Auth::user()->id) {
            return $user;
        } else {
            return $this->belongsTo(User::class, 'contact_two', 'id');
        }
    }

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

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

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


protected static function booted()
    {
        static::addGlobalScope(new OrganisationScope);

        // Automatically set the organisation_id before creating the model
        static::creating(function ($model) {
            if (Auth::check()) {
                $model->organisation_id = Auth::user()->organisation_id;
            }
        });
    }