<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use App\Traits\ModelTimeFormat;
use Illuminate\Support\Facades\Cache;

use App\Traits\ModelLogChanging;

class UserLabor extends Model
{
    use ModelLogChanging;

    use ModelTimeFormat;
    protected $primaryKey='user_id';
    public $incrementing=false;
    public $timestamps=false;

    protected $fillable=[
        'user_id','default_hour_price','labor_company_id','present_status','user_workgroup_id'
    ];
    protected $appends = [
        'is_relieve_facility',
    ];

    public function userDetail(){
        return $this->belongsTo('App\UserDetail','user_id','user_id');
    }
    public  function laborCompany(){
        return $this->belongsTo('App\LaborCompany');
    }
    public function getIsRelieveFacilityAttribute(){
        return Cache::has('dutyCheckTokenUser_'.$this['user_id']);
    }

    public function isOccupiedAt($date,$time):bool{
        $processDailyParticipants=ProcessDailyParticipant::query()->select('ended_at')->where('user_id',$this['user_id'])
            ->whereHas('processDaily', function ($query) use($date){
            $query->where('date',$date);
        })->get();
        foreach ($processDailyParticipants as $processDailyParticipant){
            if (Carbon::parse($processDailyParticipant->ended_at)->lt(Carbon::parse($time))){
                return true;
            }
        }
        return false;
    }
}
