<?php

namespace objects;

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

class xlvask_vehicle_types_o extends db
{
    use db_object_t;

    public object_property $vehicleTypeId; // The vehicle type id (In XL Vask)
    public object_property $name; // The name of the booking type
    public object_property $product; // The product id
    public object_property $created_at; // The created at timestamp
    public object_property $updated_at; // The updated at timestamp

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

    /**
     * Convert the object to an array
     * @throws Exception If the object is not selected
     * @throws Exception If the object is not found
     */
    public function asArray(): array
    {
        self::requireSelected();
        return [
            'id' => (int)$this->id,
            'vehicleTypeId' => (int)$this->vehicleTypeId->value(),
            'name' => $this->name->value(),
            'product' => (int)$this->product->value(),
            'created_at' => (int)$this->created_at->value(),
            'updated_at' => (int)$this->updated_at->value(),
        ];
    }

    /**
     * Add a new object to the database
     * @param string $vehicleTypeId The vehicle type id (In XL Vask)
     * @param string|null $name The name of the booking type
     * @param int $product The product id
     * @param int $created_at The created at timestamp
     * @param int $updated_at The updated at timestamp
     * @return void
     * @throws Exception If the object was not created successfully
     */
    public function add(
        string  $vehicleTypeId,
        ?string $name = null,
        int     $product = 0,
        int     $created_at = 0,
        int     $updated_at = 0
    ): void
    {
        $tmp_id = self::add_object([
            'vehicleTypeId' => $vehicleTypeId,
            'name' => $name,
            'product' => (int)$product,
            'created_at' => (int)$created_at,
            'updated_at' => (int)$updated_at,
        ]);
        $this->id = $tmp_id;
        self::getObjectProperties();
        self::objectChanged();
    }

    public function getObjectProperties(): void
    {
        $this->vehicleTypeId = new object_property($this->table, $this->id, 'vehicleTypeId', 'string', true);
        $this->name = new object_property($this->table, $this->id, 'name', 'string', true);
        $this->product = new object_property($this->table, $this->id, 'product', 'int', true);
        $this->created_at = new object_property($this->table, $this->id, 'created_at', 'int', true);
        $this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'int', true);
    }

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