<?php

namespace objects;

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

class edge_gateway_operations_o extends db
{
    use db_object_t;

    public object_property $gateway_id;
    public object_property $type;
    public object_property $operation_type;
    public object_property $status;
    public object_property $request_json;
    public object_property $summary_json;
    public object_property $result_json;
    public object_property $error_code;
    public object_property $error_message;
    public object_property $correlation_id;
    public object_property $agent_instance_id;
    public object_property $lease_expires_at;
    public object_property $last_progress_at;
    public object_property $attempt_count;
    public object_property $requested_by;
    public object_property $requested_at;
    public object_property $started_at;
    public object_property $completed_at;
    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_operations');
    }

    public function getObjectProperties(): void
    {
        $this->gateway_id = new object_property($this->table, $this->id, 'gateway_id', 'int', false);
        $this->type = new object_property($this->table, $this->id, 'type', 'string', false);
        $this->operation_type = new object_property($this->table, $this->id, 'operation_type', 'string', false);
        $this->status = new object_property($this->table, $this->id, 'status', 'string', false);
        $this->request_json = new object_property($this->table, $this->id, 'request_json', 'json', false);
        $this->summary_json = new object_property($this->table, $this->id, 'summary_json', 'json', false);
        $this->result_json = new object_property($this->table, $this->id, 'result_json', 'json', false);
        $this->error_code = new object_property($this->table, $this->id, 'error_code', 'string', false);
        $this->error_message = new object_property($this->table, $this->id, 'error_message', 'text', false);
        $this->correlation_id = new object_property($this->table, $this->id, 'correlation_id', 'string', false);
        $this->agent_instance_id = new object_property($this->table, $this->id, 'agent_instance_id', 'string', false);
        $this->lease_expires_at = new object_property($this->table, $this->id, 'lease_expires_at', 'string', false);
        $this->last_progress_at = new object_property($this->table, $this->id, 'last_progress_at', 'string', false);
        $this->attempt_count = new object_property($this->table, $this->id, 'attempt_count', 'int', false);
        $this->requested_by = new object_property($this->table, $this->id, 'requested_by', 'int', false);
        $this->requested_at = new object_property($this->table, $this->id, 'requested_at', 'string', false);
        $this->started_at = new object_property($this->table, $this->id, 'started_at', 'string', false);
        $this->completed_at = new object_property($this->table, $this->id, 'completed_at', 'string', 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(),
            'type' => (string)($this->type->value() ?? $this->operation_type->value() ?? ''),
            'status' => (string)$this->status->value(),
            'request' => (array)($this->request_json->value() ?? []),
            'summary' => (array)($this->summary_json->value() ?? []),
            'result' => (array)($this->result_json->value() ?? []),
            'error_code' => $this->error_code->value() === null ? null : (string)$this->error_code->value(),
            'error_message' => $this->error_message->value() === null ? null : (string)$this->error_message->value(),
            'correlation_id' => (string)$this->correlation_id->value(),
            'agent_instance_id' => $this->agent_instance_id->value() === null ? null : (string)$this->agent_instance_id->value(),
            'lease_expires_at' => $this->lease_expires_at->value() === null ? null : (string)$this->lease_expires_at->value(),
            'last_progress_at' => $this->last_progress_at->value() === null ? null : (string)$this->last_progress_at->value(),
            'attempt_count' => (int)($this->attempt_count->value() ?? 0),
            'requested_by' => $this->requested_by->value() === null ? null : (int)$this->requested_by->value(),
            'requested_at' => (string)$this->requested_at->value(),
            'started_at' => $this->started_at->value() === null ? null : (string)$this->started_at->value(),
            'completed_at' => $this->completed_at->value() === null ? null : (string)$this->completed_at->value(),
            'created_at' => (string)$this->created_at->value(),
            'updated_at' => $this->updated_at->value() === null ? null : (string)$this->updated_at->value(),
        ];
    }
}
