<?php

namespace motorapi\actions;

use Exception;
use traits\module_action_t;

class license_plate_lookup_a
{
    use module_action_t;

    /**
     * @inheritDoc
     * @throws Exception If the module name is invalid
     */
    public function run(): void
    {
        self::set_module_name('MOTORAPI');

        /** Add the action to the list of valid actions */
        // LICENSE_PLATE_LOOKUP
        self::add_action(
            'LICENSE_PLATE_LOOKUP',
            'This action is when a license plate lookup request is made',
            // Method to call
            self::class . '::license_plate_lookup',
            [
                'license_plate' => 'The license plate that was looked up',
                'data' => 'The data returned from the lookup (If any)',
                'status_code' => 'The status code of the action',
            ]
        );
    }

    /**
     * License plate lookup action
     * @param string $license_plate The license plate that was looked up
     * @param array|object $data The data returned from the lookup (If any)
     * @param int $status_code The status code of the action
     * @throws Exception If the action name is invalid
     * @throws Exception If the action data is invalid
     * @throws Exception If the action status code is invalid
     * @throws Exception If the action is not valid
     */
    public function license_plate_lookup(string $license_plate, array|object $data = [], int $status_code = 0): void
    {
        self::set_action('LICENSE_PLATE_LOOKUP');
        // If the data is an object, convert it to an array
        if (is_object($data)) {
            $data = (array)$data;
        }
        // Validate the action data
        if (!is_array($data)) {
            throw new Exception('Action data is invalid');
        }
        // Validate the action status code
        self::set_data([
            'license_plate' => $license_plate,
            'data' => $data,
        ]);
        self::set_status($status_code);
        // Add the action to the log
        self::add_action_log(
            self::get_action_name(),
            (int)self::get_status(),
        );
    }
}