<?php

namespace objects;

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

class edge_gateway_relay_bindings_o extends db
{
    use db_object_t;

    public object_property $gateway_id;
    public object_property $department_id;
    public object_property $relay_id;
    public object_property $device_id;
    public object_property $local_ip;
    public object_property $channel;
    public object_property $binding_source;
    public object_property $approved_by;
    public object_property $approved_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_relay_bindings');
    }

    public function getObjectProperties(): void
    {
        $this->gateway_id = new object_property($this->table, $this->id, 'gateway_id', 'int', false);
        $this->department_id = new object_property($this->table, $this->id, 'department_id', 'int', false);
        $this->relay_id = new object_property($this->table, $this->id, 'relay_id', 'string', 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->channel = new object_property($this->table, $this->id, 'channel', 'int', false);
        $this->binding_source = new object_property($this->table, $this->id, 'binding_source', 'string', false);
        $this->approved_by = new object_property($this->table, $this->id, 'approved_by', 'int', false);
        $this->approved_at = new object_property($this->table, $this->id, 'approved_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();
        $metadata = (array)($this->metadata_json->value() ?? []);

        return [
            'id' => (int)$this->id,
            'gateway_id' => (int)$this->gateway_id->value(),
            'department_id' => (int)$this->department_id->value(),
            'relay_id' => (string)$this->relay_id->value(),
            'device_id' => (string)$this->device_id->value(),
            'local_ip' => $this->local_ip->value() === null ? null : (string)$this->local_ip->value(),
            'channel' => (int)$this->channel->value(),
            'binding_source' => (string)$this->binding_source->value(),
            'approved_by' => $this->approved_by->value() === null ? null : (int)$this->approved_by->value(),
            'approved_at' => $this->approved_at->value() === null ? null : (string)$this->approved_at->value(),
            'metadata' => $metadata,
            'fallback_mode' => isset($metadata['fallback_mode']) ? (string)$metadata['fallback_mode'] : 'PREFER_LOCAL',
            'last_resolution' => isset($metadata['last_resolution']) && is_array($metadata['last_resolution'])
                ? (array)$metadata['last_resolution']
                : null,
            'last_success_at' => isset($metadata['last_success_at']) ? (string)$metadata['last_success_at'] : null,
            'last_error' => isset($metadata['last_error']) ? (string)$metadata['last_error'] : null,
            'created_at' => (string)$this->created_at->value(),
            'updated_at' => $this->updated_at->value() === null ? null : (string)$this->updated_at->value(),
        ];
    }
}
