<?php

namespace App\Modules\Bookings;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use App\Models\ValidationModel;
use App\Modules\Bookings\Task;
use App\Modules\Bookings\Project;
use App\Modules\Bookings\Message;
use App\Models\User;

class Comment extends Model
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['related_id', 'related_type','type','message', 'created_by', 'read', 'emailed_to', 'sms_to', 'from_name', 'from_address', 'public', 'customer_reply', 'auto_booked','style', 'created_by'];

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'comments';

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'related_id'   => 'required',
        'related_type' => 'required',
        'message'      => 'required_if:type,task',
    ];

    /**
     * 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) {
            if (! $model->created_by) {
                $model->created_by = getAuthUserId();
            }
        });
    }

    /**
     * Returns the related model.
     *
     * @return App\Modules\Bookings\Task or App\Modules\Clients\Customer depending on type.
     */
     public function related()
     {
       if ($this->related_type == 'task')
       {
         return $this->belongsTo(Task::class, 'related_id', 'id');
       }
       else if ($this->related_type == 'project')
       {
         return $this->belongsTo(Project::class, 'related_id', 'id');
       }
       else
       {
         return $this->belongsTo(Message::class, 'related_id', 'id');
       }
     }

    /**
     * Filters results to this months interval date.
     *
     * @return Query
     */
    public function scopeThisMonth($query, $start_date = '', $end_date = '')
    {
        if ($start_date && $end_date) {
            $start_date = is_numeric($start_date) ? 
                now()->setTimestamp($start_date) : 
                now()->parse($start_date);
            $end_date = is_numeric($end_date) ? 
                now()->setTimestamp($end_date) : 
                now()->parse($end_date);

            return $query->whereBetween('created_at', [$start_date, $end_date]);
        }

        return $query->whereBetween('created_at', [
            now()->startOfMonth(),
            now()->endOfMonth()
        ]);
    }

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

    /**
     * Alias for creator() - returns the user who created the comment.
     * This maintains compatibility with frontend code that expects comment.user
     *
     * @return \User
     */
    public function user()
    {
        return $this->creator();
    }

    /**
     * Filters comments to unread messages.
     *
     * @return query
     */
    public function scopeUnread($query)
    {
        return $query->where('read', '=', 0);
    }

    /**
     * Filters comments to read messages.
     *
     * @return query
     */
    public function scopeRead($query)
    {
        return $query->where('read', '=', 1);
    }
}
