<?php

namespace App\Models;
use App\Models\ValidationModel;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id', 'action', 'object_type', 'object_id', 'description'];

    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'user_id'    => 'required',
        'action'     => 'required',
        'object_type' => 'required',
        'object_id'  => 'required',
    ];

    /**
     * Automatically append created_by.
     *
     * @var 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;
        });
    }

    /**
     *	Decode the attachment when retrieved from the database.
     *
     *	@return string / null
     */
    public function getAttachmentAttribute($value)
    {
        return $value ? json_decode($value) : null;
    }

    /**
     *	Restrict the results to the given object.
     *
     *	@return Doctrine\Dbal\Query\QueryBuilder
     */
    public function scopeFilter($query, $subject_type, $subject_id)
    {
        return $query->where('subject_type', $subject_type)->where('subject_id', $subject_id);
    }

    /**
     * Restrict results to type project only.
     * Optionally you can pass the id of the project
     * to limit results even further.
     *
     * @return Doctrine\Dbal\Query\QueryBuilder
     */
    public function scopeProject($query, $id = null)
    {
        $query->where('subject_type', 'project');

        if ($id) {
            $query->where('subject_id', $id);
        }

        return $query;
    }

    /**
     * Restrict results to the specified action.
     *
     * @return Doctrine\Dbal\Query\QueryBuilder
     */
    public function scopeAction($query, $action = '')
    {
        if (! empty($action)) {
            return $query->where('action', $action);
        } else {
            return $query;
        }
    }

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

    /**
     * Get the user that performed this action.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the polymorphic relationship.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function object()
    {
        return $this->morphTo();
    }
}
