<?php

namespace shelly\actions;

use Exception;
use traits\module_action_t;

class shelly_search_a
{
    use module_action_t;

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

        /** Add the action to the list of valid actions */
        // Search
        self::add_action(
            'SHELLY_SEARCH',
            'This action is when a search is performed',
            // Method to call
            self::class . '::shelly_search',
            [
                'search' => 'The search term',
                'data' => 'The data returned from the conversion (If any)',
                'status_code' => 'The status code of the action',
            ]
        );
    }

    /**
     * Search action
     * @param array|object $data The data returned from the conversion (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 shelly_search(array|object $data = [], int $status_code = 0): void
    {
        self::set_action('SHELLY_SEARCH');
        // 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($data);
        self::set_status($status_code);
        // Add the action to the log
        self::add_action_log(
            self::get_action_name(),
            (int)self::get_status(),
        );
    }
}