<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Token extends Model
{
     protected $fillable = ['access_token','examiner_id','scheduleId'];
    
  	 public function is_table_empty($scheduleId) {
  	     
        $token = static::where('examiner_id',\Auth::user()->id)->where('scheduleId',$scheduleId)->first('access_token');
        
        if($token) {
            return false;
        }
        return true;
    }

    public function get_access_token($scheduleId) {
    	$token = static::where('examiner_id',\Auth::user()->id)->where('scheduleId',$scheduleId)->first('access_token');
        return json_decode($token);
    }
  
    public function get_refersh_token() {
        $result = $this->get_access_token();
        return $result;
    }
  
    public function update_access_token($token,$scheduleId) {
        if($this->is_table_empty($scheduleId)) {
            return static::create(['access_token' => $token, 'examiner_id' => \Auth::user()->id,'scheduleId' => $scheduleId]);
        } else {
            return static::where('examiner_id',\Auth::user()->id)->where('scheduleId',$scheduleId)->update(['access_token' => $token]);
        }
    }
    
    public function deletes($id,$scheduleId){
        return static::where('examiner_id',$id)->where('scheduleId',$scheduleId)->delete();
    }
}