<?php

namespace App\Modules\Hr;

use Illuminate\Support\Facades\DB;
use App\Models\ValidationModel;
use App\Models\User;
use App\Modules\Hr\TimeLog;
use App\Modules\Hr\TimeLogChangeRequest;
use App\Modules\Hr\Holiday;

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

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'user_id';

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

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id', 'type', 'time', 'address', 'address2', 'postcode', 'department_id', 'national_insurance', 'rate_per_hour', 'annual_leaves', 'default_start_time', 'default_end_time'];

    /**
     * Returns the total number of holiday days left for the current year.
     *
     * @return int
     */
    public function getPaidHolidayDaysLeftAttribute()
    {
        return $this->attributes['annual_leaves'] - Holiday::query()->where('user_id', $this->user_id)->where('status', 'approved')->where('start_date', '>=', date('Y-01-01 00:00:00'))->where('end_date', '<=', date('Y-12-31 23:59:59'))->where('paid', 1)->sum('total_days');
    }

    /**
     * Returns the list of available departments.
     *
     * @return array
     */
    public static function departments()
    {
        return [
            1 => 'Repairs',
            2 => 'Remote Support',
            3 => 'Onsite',
            4 => 'Development',
            5 => 'Front Desk',
            6 => 'Accounts',
            7 => 'Management',
            8 => 'Sales',
        ];
    }

    /**
     * Returns the inline address of the user.
     *
     * @return string
     */
    public function getInlineAddressAttribute()
    {
        $address = [];
        if ($this->attributes['address']) {
            $address[] = $this->attributes['address'];
        }
        if ($this->attributes['address2']) {
            $address[] = $this->attributes['address2'];
        }
        if ($this->attributes['postcode']) {
            $address[] = $this->attributes['postcode'];
        }

        return count($address) ? implode(', ', $address) : '';
    }

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

    /**
     * Returns all the pending change requests for the time log.
     *
     * @return Illuminate\Support\Collection
     */
    public function latest_time_log()
    {
        return $this->hasOne(TimeLog::class, 'user_id')
            ->whereDate('time', now()->toDateString())
            ->latest('time');
    }

    public function time_logs()
    {
        return $this->hasMany(TimeLog::class, 'user_id');
    }

    public function pending_change_requests()
    {
        return $this->hasMany(TimeLogChangeRequest::class, 'created_by')->where('completed', 0);
    }
}
