<?php

namespace WPD_Platform\Integrations\Wordpress\Actions;

use Dollie\SDK\Attributes\Action;
use Dollie\SDK\Base\AutomateAction;
use Dollie\SDK\Traits\SingletonLoader;
use Exception;

#[Action(
    id: 'find_user_meta_by_key',
    label: 'User: Find User meta by key',
    since: '1.0.0'
)]
/**
 * FindUserMetaByKey.
 * php version 5.6
 *
 * @category FindUserMetaByKey
 * @author   BSF <username@example.com>
 * @license  https://www.gnu.org/licenses/gpl-3.0.html GPLv3
 * @link     https://www.brainstormforce.com/
 * @since    1.0.0
 */
/**
 * FindUserMetaByKey
 *
 * @category FindUserMetaByKey
 * @author   BSF <username@example.com>
 * @license  https://www.gnu.org/licenses/gpl-3.0.html GPLv3
 * @link     https://www.brainstormforce.com/
 * @since    1.0.0
 */
class FindUserMetaByKey extends AutomateAction
{
    use SingletonLoader;

    /**
     * Integration type.
     *
     * @var string
     */
    public $integration = 'WordPress';

    /**
     * Action name.
     *
     * @var string
     */
    public $action = 'find_user_meta_by_key';

    /**
     * Register action.
     *
     * @param array $actions action data.
     * @return array
     */
    public function register($actions)
    {
        $actions[$this->integration][$this->action] = [
            'label' => __('User: Find User meta by key', 'dollie'),
            'action' => 'find_user_meta_by_key',
            'function' => [$this, 'action_listener'],
        ];

        return $actions;
    }

    /**
     * Action listener.
     *
     * @param int   $user_id user_id.
     * @param int   $automation_id automation_id.
     * @param array $fields fields.
     * @param array $selected_options selected_options.
     * @return array|bool
     * @throws Exception Exception.
     */
    public function _action_listener($user_id, $automation_id, $fields, $selected_options)
    {
        $dynamic_response = [];
        if (empty($user_id)) {
            $email = $selected_options['wp_user_email'];
            $user = get_user_by('email', $email);
            if ($user) {
                $user_id = $user->ID;
            }
        }
        if (! empty($selected_options['meta_key'])) {
            $meta_key = $selected_options['meta_key'];
        } else {
            $meta_key = '';
        }

        if ('' !== $meta_key) {
            $meta_value = get_user_meta($user_id, $meta_key, true);
            $dynamic_response[] = [
                'user_id' => $user_id,
                'meta_key' => $meta_key,
                'meta_value' => $meta_value,
            ];

        }

        return $dynamic_response;
    }
}

FindUserMetaByKey::get_instance();
