<?php

namespace objects;

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

class department_selfserve_vehicle_conditions_o extends db
{
    use db_object_t;

    public object_property $department; // The department id
    public object_property $lane; // The lane id
    public object_property $customer_id; // The customer id
    public object_property $reg; // The vehicle registration number
    public object_property $question; // The question id
    public object_property $value; // The answer value
    public object_property $created_at;
    public object_property $updated_at;
    public object_property $deleted_at;


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


    /**
     * Add a department lane
     * @param int $department The department id
     * @param int $lane The lane id
     * @param string $reg The vehicle registration number
     * @param int $question The question id
     * @param bool $value The answer value
     * @param int|null $customer_id The customer id
     * @return department_selfserve_vehicle_conditions_o
     * @throws Exception If the object was not created successfully
     */
    public function add(int $department, int $lane, string $reg, int $question, bool $value, ?int $customer_id = null): department_selfserve_vehicle_conditions_o
    {
        global /** @var db $db */
        $db;
        // Sanitize the input
        $department = (int)$department;
        $lane = (int)$lane;
        $reg = $db->escape_string($reg);
        // Standardize the registration number
        $reg = selfserve::standardize_registration($reg);
        $question = (int)$question;
        $value = (bool)$value;
        $customer_id = $customer_id !== null ? (int)$customer_id : null;
        // Remove any existing entry for the same department, lane, reg and question
        $sql = "DELETE FROM $this->table WHERE department = $department AND lane = $lane AND reg = '$reg' AND question = $question";
        $db->query($sql);
        // Add the object
        $tmp_id = self::add_object([
            'department' => $department,
            'lane' => $lane,
            'reg' => $reg,
            'question' => $question,
            'value' => $value,
            'customer_id' => $customer_id,
        ]);
        $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->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', false);
        $this->reg = new object_property($this->table, $this->id, 'reg', 'string', false);
        $this->question = new object_property($this->table, $this->id, 'question', 'int', false);
        $this->value = new object_property($this->table, $this->id, 'value', 'bool', 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 asArray(): array
    {
        return [
            'id' => (int)$this->id,
            'department' => (int)$this->department->value(),
            'lane' => (int)$this->lane->value(),
            'customer_id' => is_null($this->customer_id->value()) ? null : (int)$this->customer_id->value(),
            'reg' => (string)$this->reg->value(),
            'question' => (int)$this->question->value(),
            'value' => (bool)$this->value->value(),
            'created_at' => (string)$this->created_at->value(),
            'updated_at' => (string)$this->updated_at->value(),
            'deleted_at' => is_null($this->deleted_at->value()) ? null : (string)$this->deleted_at->value(),
        ];
    }
}