<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\Auth;
use App\Models\ValidationModel;
use App\Modules\Clients\Client;
use App\Modules\Clients\Customer;
use App\Models\User;
use App\Modules\Bookings\Task;

class CustomerNotification extends ValidationModel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'customer_notifications';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['type', 'sent_to', 'task_id', 'message', 'subject'];

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();

        // When a transaction is created we want to
        // update the created_by field
        static::creating(function ($model) {
            $model->created_by = Auth::user()->id;
        });
    }

    /**
     * Returns the client model.
     *
     * @return App\Modules\Clients\Client or App\Modules\Clients\Customer depending on type.
     */
    public function client()
    {
        if ($this->attributes['client_type'] == 'client') {
            return $this->belongsTo(Client::class, 'client_id');
        } else {
            return $this->belongsTo(Customer::class, 'client_id');
        }
    }

    /**
     * Returns the user who created the comment.
     *
     * @return \User
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Returns the task which this notification has been sent for.
     *
     * @return \Task
     */
    public function task()
    {
        return $this->belongsTo(Task::class, 'task_id');
    }
}
