<?php

namespace SHServ\Entities;

class DeviceAuth extends \SHServ\Middleware\Entity {
	public static $table_name = "device_auth";
	protected static $fields = [
		"id", "device_id", "device_token", "status", "create_at"
	];

	protected ?Device $device_instance = null;

	public function __construct(Int $id, Array $data = [], ?Device $device = null){
		parent::__construct(
			self::$table_name,
			$id,
			$data
		);
		$this -> device_instance = $device;
	}

	public function remove() {
		return $this -> remove_entity();
	}

	public function kill() {
		$this -> status = "killed";
		$result = $this -> update();
		if($this -> device_instance) {
			$this -> device_instance -> clear_device_auth_cache();
		}
		return $result;
	}

	public function is_active() {
		return $this -> status == "active";
	}
}