<?php

namespace objects;

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

class form_submissions_o extends db
{
    use db_object_t;

    public object_property $form_identifier;
    public object_property $data;
    public object_property $user_id;
    public object_property $customer_number;
    public object_property $department_id;
    public object_property $created_at;


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

    /**
     * Add a form submission object, and set this object to the new object
     * @param string $form_identifier The form identifier
     * @param array $data The form data
     * @param int|null $user_id The user id (If applicable)
     * @param int|null $customer_number The customer number (If applicable)
     * @param int|null $department_id The department id (If applicable)
     * @return void
     * @throws Exception If the object was not created successfully
     */
    public function add(string $form_identifier, array $data, ?int $user_id = null, ?int $department_id = null, ?int $customer_number = null): void
    {
        global /** @var db $db */
        $db;
        // Sanitize the input
        $form_identifier = $db->escape_string($form_identifier);
        $encoded_data = $db->escape_string(json_encode($data));
        // Add the object
        $tmp_arr = [
            'form_identifier' => $form_identifier,
            'data' => $encoded_data,
        ];
        // Add the user id and department id if they are set
        if (!empty($user_id)) {
            $tmp_arr['user_id'] = (int)$user_id;
        }
        if (!empty($customer_number)) {
            $tmp_arr['customer_number'] = (int)$customer_number;
        }
        if (!empty($department_id)) {
            $tmp_arr['department_id'] = (int)$department_id;
        }
        $tmp_id = self::add_object($tmp_arr);
        $this->id = $tmp_id;
        self::getObjectProperties();
        self::objectChanged();
    }

    public function getObjectProperties(): void
    {
        $this->form_identifier = new object_property($this->table, $this->id, 'form_identifier', 'string', false);
        $this->data = new object_property($this->table, $this->id, 'data', 'string', false);
        $this->user_id = new object_property($this->table, $this->id, 'user_id', 'int', false);
        $this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', false);
        $this->department_id = new object_property($this->table, $this->id, 'department_id', 'int', false);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'string', false);
    }

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


    public function asArray(): array
    {
        return [
            'id' => (int)$this->id,
            'form_identifier' => (string)$this->form_identifier->value(),
            'data' => (string)$this->data->value(),
            'user_id' => (int)$this->user_id->value(),
            'customer_number' => (int)$this->customer_number->value(),
            'department_id' => (int)$this->department_id->value(),
            'created_at' => (string)$this->created_at->value(),
        ];
    }

}