<?php

namespace objects;

use classes\db;
use classes\object_property;
use traits\db_object_t;

class plate_scanners_o extends db
{
    use db_object_t;

    public object_property $department_id;
    public object_property $name;
    public object_property $notes;
    public object_property $api_key;

    public function structure(): void
    {
        $this->setTable('plate_scanners');
    }

    public function objectChanged(): void
    {
        // No need to invalidate the cache, since the plate_scanners object is not cached
    }

    public function getPlateScannerById(int $id): plate_scanners_o
    {
        global $db;
        // Get the record from the database
        $sql = "SELECT * FROM $this->table WHERE id = $id";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            $this->id = $id;
            $this->getObjectProperties();
        }
        return $this;
    }

    public function getObjectProperties(): void
    {
        $this->department_id = new object_property($this->table, $this->id, 'department_id', 'int');
        $this->name = new object_property($this->table, $this->id, 'name', 'string');
        $this->notes = new object_property($this->table, $this->id, 'notes', 'string');
        $this->api_key = new object_property($this->table, $this->id, 'api_key', 'string');
    }

    public function add(int $department_id, string $name, string $notes): void
    {
        global $db, $response;
        try {
            // Generate an API key
            $api_key = bin2hex(random_bytes(32));
            // Avoid SQL injection
            $name = $db->escape_string($name);
            $notes = $db->escape_string($notes);
            // Create a new record in the database
            $sql = "INSERT INTO $this->table (department_id, name, notes, api_key) VALUES ($department_id, '$name', '$notes', '$api_key')";
            $db->query($sql);

            // Get the id of the new record
            $this->id = $db->insert_id();

            // Set the values of the object properties
            $this->getObjectProperties();
        } catch (\Exception $e) {
            $response->error($e->getMessage());
        }
    }

    public function edit(int $id, int $department_id, string $name, string $notes): void
    {
        global $db, $response;
        $this->id = $id;
        try {
            // Avoid SQL injection
            $name = $db->escape_string($name);
            $notes = $db->escape_string($notes);
            // Update the record in the database
            $sql = "UPDATE $this->table SET department_id = $department_id, name = '$name', notes = '$notes' WHERE id = $id";
            $db->query($sql);

            // Set the values of the object properties
            $this->getObjectProperties();
        } catch (\Exception $e) {
            $response->error($e->getMessage());
        }
    }

    public function getPlateScannerByApiKey(mixed $token): plate_scanners_o
    {
        global $db;
        // Avoid SQL injection
        $token = $db->escape_string($token);
        // Get the record from the database
        $sql = "SELECT * FROM $this->table WHERE api_key = '$token'";
        $result = $db->query($sql);
        if ($result->num_rows > 0) {
            $this->id = $result->fetch_assoc()['id'];
            $this->getObjectProperties();
        }
        return $this;
    }
}