<?php

namespace objects;

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

class notifications_o extends db
{
    use db_object_t;

    /**
     * The notification type
     * @var object_property $type The notification type
     */
    public object_property $type;
    /**
     * The user id
     * @var object_property $user_id The user id
     */
    public object_property $user_id;
    /**
     * The notification data
     * @var object_property $data The notification data (JSON encoded)
     */
    public object_property $data;
    /**
     * The notification created at
     * @var object_property $created_at The notification created at
     */
    public object_property $created_at;


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

    /**
     * Add a notification object, and set this object to the new object
     * @param string $type The notification type id
     * @param int $user_id The user id
     * @param array $data The notification data
     * @return void
     * @throws Exception If the object was not created successfully
     */
    public function add(string $type, int $user_id, array $data): void
    {
        global /** @var db $db */
        $db;
        // Sanitize the input
        $type = $db->escape_string($type);
        $user_id = (int)$user_id;
        // JSON encode the data
        $encoded_data = $db->escape_string(json_encode($data));
        // Add the object
        $tmp_id = self::add_object([
            'type' => $type,
            'user_id' => $user_id,
            'data' => $encoded_data,
        ]);
        if (!$tmp_id) {
            throw new Exception('The object was not created successfully.');
        }
        $this->id = $tmp_id;
        self::getObjectProperties();
        self::objectChanged();
    }

    /**
     * Get the object properties
     * @return void
     */
    public function getObjectProperties(): void
    {
        $this->type = new object_property($this->table, $this->id, 'type', 'string', false);
        $this->user_id = new object_property($this->table, $this->id, 'user_id', 'int', false);
        $this->data = new object_property($this->table, $this->id, 'data', 'string', false);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'datetime', false);
    }

    public function objectChanged(): void
    {
        //TODO: Add cache invalidation
    }

    /**
     * Get the object as an array
     * @return array The object as an array
     */
    public function asArray(): array
    {
        return [
            'id' => (int)$this->id,
            'type' => (string)$this->type->value(),
            'user_id' => (int)$this->user_id->value(),
            'data' => self::decodeData((string)$this->data->value()),
            'created_at' => (string)$this->created_at->value(),
        ];
    }

    /**
     * Decode the data
     * @param string $data The data JSON encoded
     * @return array The data
     */
    public static function decodeData(string $data): array
    {
        return json_decode($data, true);
    }
}