<?php

namespace objects;

use classes\db;
use classes\object_property;
use classes\xlvask;
use Exception;
use helpers\xlvask_customer;
use helpers\xlvask_vehicle;
use traits\db_object_t;

class xlvask_vehicles_o extends db
{
    use db_object_t;

    public object_property $vehicleId;
    public object_property $registrationNumber;
    public object_property $customerId;
    public object_property $vehicleTypeId;
    public object_property $active;
    public object_property $autoStartOnLpr;
    public object_property $updated;
    public object_property $createDate;
    public object_property $externId;

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

    /**
     * Add a new XL Vask customer
     * @param array $data The properties
     * @returns void
     * @throws Exception If the object was not created successfully
     */
    public function add(array $data): void
    {
        $tmp_id = self::add_object($data);
        $this->id = $tmp_id;
        self::getObjectProperties();
        self::objectChanged();
    }

    public function getObjectProperties(): void
    {
        $this->vehicleId = new object_property($this->table, $this->id, 'vehicleId', 'string', false);
        $this->registrationNumber = new object_property($this->table, $this->id, 'registrationNumber', 'string', false);
        $this->customerId = new object_property($this->table, $this->id, 'customerId', 'string', false);
        $this->vehicleTypeId = new object_property($this->table, $this->id, 'vehicleTypeId', 'string', false);
        $this->active = new object_property($this->table, $this->id, 'active', 'string', false);
        $this->autoStartOnLpr = new object_property($this->table, $this->id, 'autoStartOnLpr', 'string', false);
        $this->updated = new object_property($this->table, $this->id, 'updated', 'string', false);
        $this->createDate = new object_property($this->table, $this->id, 'createDate', 'string', false);
        $this->externId = new object_property($this->table, $this->id, 'externId', 'string', false);
    }

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

    /**
     * Import the vehicles from XL Vask
     * @throws Exception If the objects were not successfully added.
     * @returns void
     */
    public function importVehicles(): void
    {
        if (!empty($this->id)) {
            throw new Exception('To prevent issues, having a selected object is not allowed.');
        }
        $vehicles = $this->getVehiclesFromXLVask();
        /** @var string[] $known_vehicleIds The XL Vask vehicleIds currently known */
        $known_vehicleIds = array_map(function ($vehicle) {
            return $vehicle['vehicleId'];
        }, self::getFields(['vehicleId']));
        /** The XL Vask customers without a matching vehicleId in the database */
        $new_vehicles = array_filter($vehicles, function ($vehicle) use ($known_vehicleIds) {
            return !in_array($vehicle->vehicleId, $known_vehicleIds);
        });
        unset($known_vehicleIds);
        unset($vehicles);
        /** Adding the new vehicles */
        foreach ($new_vehicles as $vehicle) {
            if (!$vehicle->isValid()) {
                echo $vehicle->formattedDetails();
                throw new Exception('A vehicle from XL Vask is not valid, have the structure changed?');
            }
        }
        // Actually save the vehicle
        foreach ($new_vehicles as $vehicle) {
            $this->add($vehicle->toArray());
        }
        unset($new_vehicles);
    }

    /**
     * @returns xlvask_vehicle[]
     * @throws Exception
     */
    public function getVehiclesFromXLVask(): array
    {
        /** Get the vehicles from XL Vask */
        $xlvask = new xlvask();
        $vehicle_class = $xlvask->new($xlvask->helpers->xlvask_vehicle);
        /** @var xlvask_vehicle[] $vehicles */
        $vehicles = $vehicle_class->toObjects($xlvask->getVehicles());
        return $vehicles;
    }
}