<?php

namespace objects;

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

class department_selfserve_questions_o extends db
{
    use db_object_t {
        delete as trait_delete;
    }

    public object_property $department; // The department id
    public object_property $lane; // The lane id
    public object_property $product; // The product id
    public object_property $condition_id; // The question condition object id (if applicable)
    public object_property $question; // The question text
    public object_property $description; // The question description
    public object_property $order_priority; // The order priority of the question (lower numbers are shown first)
    public object_property $created_at;
    public object_property $updated_at;
    public object_property $deleted_at;


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

    /**
     * Add a question
     * @param int $department The department id
     * @param int $lane The lane id
     * @param int $product The product id
     * @param string $question The question text
     * @param string $description The question description
     * @param int|null $condition_id The question condition_id (if applicable)
     * @param int $order_priority The order priority of the question (lower numbers are shown first)
     * @return department_selfserve_questions_o
     * @throws Exception If the object was not created successfully
     */
    public function add(int $department, int $lane, int $product, string $question, string $description, int $condition_id = null, int $order_priority = 0): department_selfserve_questions_o
    {
        global /** @var db $db */
        $db;
        // Sanitize the input
        $department = (int)$department;
        $lane = (int)$lane;
        $product = (int)$product;
        $question = $db->escape_string($question);
        $description = $db->escape_string($description);
        if (!is_null($condition_id)) {
            $condition_id = (int)$condition_id;
        }
        $order_priority = (int)$order_priority;
        // Add the object
        $tmp_id = self::add_object([
            'department' => $department,
            'lane' => $lane,
            'product' => $product,
            'question' => $question,
            'description' => $description,
            ...(!is_null($condition_id) ? ['condition_id' => $condition_id] : []), // If the condition_id is null, it will be set to null in the database
            'order_priority' => $order_priority,
        ]);
        $this->id = $tmp_id;
        self::getObjectProperties();
        self::objectChanged();
        return $this;
    }

    public function getObjectProperties(): void
    {
        $this->department = new object_property($this->table, $this->id, 'department', 'int', false);
        $this->lane = new object_property($this->table, $this->id, 'lane', 'int', false);
        $this->product = new object_property($this->table, $this->id, 'product', 'int', false);
        $this->condition_id = new object_property($this->table, $this->id, 'condition_id', 'int', false);
        $this->question = new object_property($this->table, $this->id, 'question', 'string', false);
        $this->description = new object_property($this->table, $this->id, 'description', 'string', false);
        $this->order_priority = new object_property($this->table, $this->id, 'order_priority', 'int', 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
    {
        //TODO: Add cache invalidation
    }

    public function delete(): void
    {
        self::requireSelected();
        global $db;
        $id = (int)$this->id;
        $sql = "UPDATE department_selfserve_tasks SET condition_id = NULL WHERE condition_id = $id";
        $db->query($sql);
        $this->trait_delete();
    }

    public function asArray(): array
    {
        return [
            'id' => (int)$this->id,
            'department' => (int)$this->department->value(),
            'lane' => (int)$this->lane->value(),
            'product' => (int)$this->product->value(),
            'condition_id' => is_null($this->condition_id->value()) ? null : (int)$this->condition_id->value(),
            'question' => (string)$this->question->value(),
            'description' => (string)$this->description->value(),
            'order_priority' => (int)$this->order_priority->value(),
            // Timestamps
            'created_at' => (string)$this->created_at->value(),
            'updated_at' => (string)$this->updated_at->value(),
        ];
    }
}