<?php

namespace objects;

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

class edge_gateway_device_inventory_o extends db
{
    use db_object_t;

    public object_property $gateway_id;
    public object_property $device_id;
    public object_property $local_ip;
    public object_property $model;
    public object_property $channel_count;
    public object_property $capabilities_json;
    public object_property $online;
    public object_property $last_seen_at;
    public object_property $metadata_json;
    public object_property $created_at;
    public object_property $updated_at;
    public object_property $deleted_at;

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

    public function getObjectProperties(): void
    {
        $this->gateway_id = new object_property($this->table, $this->id, 'gateway_id', 'int', false);
        $this->device_id = new object_property($this->table, $this->id, 'device_id', 'string', false);
        $this->local_ip = new object_property($this->table, $this->id, 'local_ip', 'string', false);
        $this->model = new object_property($this->table, $this->id, 'model', 'string', false);
        $this->channel_count = new object_property($this->table, $this->id, 'channel_count', 'int', false);
        $this->capabilities_json = new object_property($this->table, $this->id, 'capabilities_json', 'json', false);
        $this->online = new object_property($this->table, $this->id, 'online', 'bool', false);
        $this->last_seen_at = new object_property($this->table, $this->id, 'last_seen_at', 'string', false);
        $this->metadata_json = new object_property($this->table, $this->id, 'metadata_json', 'json', false);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
        $this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false);
        $this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
    }

    public function objectChanged(): void
    {
    }

    public function asArray(): array
    {
        $this->requireSelected();

        return [
            'id' => (int)$this->id,
            'gateway_id' => (int)$this->gateway_id->value(),
            'device_id' => (string)$this->device_id->value(),
            'local_ip' => $this->local_ip->value() === null ? null : (string)$this->local_ip->value(),
            'model' => $this->model->value() === null ? null : (string)$this->model->value(),
            'channel_count' => (int)$this->channel_count->value(),
            'capabilities' => (array)($this->capabilities_json->value() ?? []),
            'online' => (bool)$this->online->value(),
            'last_seen_at' => $this->last_seen_at->value() === null ? null : (string)$this->last_seen_at->value(),
            'metadata' => (array)($this->metadata_json->value() ?? []),
            'created_at' => (string)$this->created_at->value(),
            'updated_at' => $this->updated_at->value() === null ? null : (string)$this->updated_at->value(),
        ];
    }
}
