<?php

namespace objects;

use classes\db;
use classes\object_property;
use classes\selfserve_schema_bootstrap;
use modules\selfserve\helpers\selfserve_wash_event_type;
use traits\db_object_t;

class selfserve_wash_session_events_o extends db
{
    use db_object_t;

    public object_property $session_id;
    public object_property $event_type;
    public object_property $payload_json;
    public object_property $created_at;

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

    public function getObjectProperties(): void
    {
        $this->session_id = new object_property($this->table, $this->id, 'session_id', 'int', false);
        $this->event_type = new object_property($this->table, $this->id, 'event_type', 'string', false);
        $this->payload_json = new object_property($this->table, $this->id, 'payload_json', 'json', false);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
    }

    public function objectChanged(): void
    {
        // No dedicated cache invalidation yet.
    }

    public function add(int $sessionId, selfserve_wash_event_type $eventType, ?array $payload = null): self
    {
        $this->id = self::add_object([
            'session_id' => $sessionId,
            'event_type' => $eventType->value,
            'payload_json' => $payload,
        ]);
        $this->getObjectProperties();
        $this->objectChanged();

        return $this;
    }

    public function listBySession(int $sessionId): array
    {
        return $this->getFieldsWhere([
            'session_id' => $sessionId,
        ], ['id', 'event_type', 'payload_json', 'created_at']);
    }
}
