<?php

namespace WPD_Platform\Integrations\Asgaros\Actions;

use Dollie\SDK\Attributes\Action;
use Dollie\SDK\Base\AutomateAction;
use Dollie\SDK\Traits\SingletonLoader;
use WPD_Platform\Integrations\WordPress\WordPress;

#[Action(
    id: 'asgaros_subscribe_user_to_forum',
    label: 'Subscribe User In Forum',
    since: '1.0.0'
)]
/**
 * SubscribeUserInForum
 *
 * @category AddTopicInForum
 * @package  DollieAI
 * @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 AsSubscribeUserInForum extends AutomateAction {

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

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

	use SingletonLoader;

	/**
	 * Register a action.
	 *
	 * @param array $actions actions.
	 * @return array
	 */
	public function register( $actions ) {
		$actions[ $this->integration ][ $this->action ] = [
			'label'    => __( 'Subscribe User In Forum', 'dollie' ),
			'action'   => $this->action,
			'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 selectedOptions.
	 * @psalm-suppress UndefinedMethod
	 * @throws Exception Exception.
	 * 
	 * @return array|bool|void
	 */
	public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
		if ( ! class_exists( 'AsgarosForum' ) ) {
			return false;
		}
		$user_email = $selected_options['wp_user_email'];
		if ( is_email( $user_email ) ) {
			$user = get_user_by( 'email', $user_email );
			if ( $user ) {
				$user_id = $user->ID;
			}
		} else {
			$error = [
				'status'   => esc_attr__( 'Error', 'dollie' ),
				'response' => esc_attr__( 'Please enter valid email address.', 'dollie' ),
			];

			return $error;
		}
		$asgaros_forum = new AsgarosForum();
		$forum_id      = $selected_options['forum_id'];
		$asgaros_forum->notifications->subscribe_forum( $forum_id );
		
		$forum = (array) $asgaros_forum->content->get_forum( $forum_id );
		$user  = WordPress::get_user_context( $user_id );
		return array_merge( $forum, $user );
	}
}

AsSubscribeUserInForum::get_instance();
