<?php

namespace App\Modules\Hr;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use App\Models\ValidationModel;
use App\Modules\Hr\TimeLog;
use App\Models\User;

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

    /**
     * The number of models to return for pagination.
     *
     * @var int
     */
    protected $perPage = 50;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['time_log_id', 'time', 'completed', 'declined'];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [];

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

        static::creating(function ($model) {
            $model->ip_address = Request::getClientIp();
            $model->created_by = Auth::user()->id;
            $model->updated_by = Auth::user()->id;
        });

        static::updating(function ($model) {
            $model->updated_by = Auth::user()->id;
        });
    }

    /**
     * Sets the value of the declined attribute as a boolean. It automatically
     * converts null to 0.
     *
     * @param void
     */
    public function setDeclinedAttribute($value)
    {
        $this->attributes['declined'] = ($value) ? 1 : 0;
    }

    /**
     * Sets the value of the completed attribute as a boolean. It automatically
     * converts null to 0.
     *
     * @param void
     */
    public function setCompletedAttribute($value)
    {
        $this->attributes['completed'] = ($value) ? 1 : 0;
    }

    /**
     * Defines the relationship with the TimeLog model.
     *
     * @return App\Modules\Hr\TimeLog
     */
    public function time_log()
    {
        return $this->belongsTo(TimeLog::class, 'time_log_id');
    }

    /**
     * Defines the relationship with the user model.
     *
     * @return \User
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Defines the relationship with the user model (alias for creator for Angular compatibility).
     *
     * @return \User
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Add a condition to restrict the results to completed records only.
     *
     * @param  \Illuminate\Database\Query\Builder $query
     *
     * @return \Illuminate\Database\Query\Builder
     */
    public function scopeCompleted($query)
    {
        return $query->where('completed', 1);
    }

    /**
     * Add a condition to restrict the results to declined records only.
     *
     * @param  \Illuminate\Database\Query\Builder $query
     *
     * @return \Illuminate\Database\Query\Builder
     */
    public function scopeDeclined($query)
    {
        return $query->where('declined', 1);
    }
}
