<?php

namespace WPD_Platform\Integrations\Buddypress\Triggers;

use Dollie\SDK\Attributes\Trigger;
use Dollie\SDK\Controllers\AutomationController;
use WPD_Platform\Integrations\WordPress\WordPress;
use Dollie\SDK\Traits\SingletonLoader;

#[Trigger(
    id: 'user_connection_request_accepted',
    label: 'A user',
    since: '1.0.0'
)]
/**
 * UserConnectionRequestAccepted.
 * php version 5.6
 *
 * @category UserConnectionRequestAccepted
 * @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
 */
/**
 * UserConnectionRequestAccepted
 *
 * @category UserConnectionRequestAccepted
 * @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
 *
 * @psalm-suppress UndefinedTrait
 */
class UserConnectionRequestAccepted
{
    use SingletonLoader;

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

    /**
     * Trigger name.
     *
     * @var string
     */
    public $trigger = 'user_connection_request_accepted';

    /**
     * Constructor
     *
     * @since  1.0.0
     */
    public function __construct()
    {
        add_filter('dollie_trigger_register_trigger', [$this, 'register']);
    }

    /**
     * Register action.
     *
     * @param array $triggers trigger data.
     * @return array
     */
    public function register($triggers)
    {

        $triggers[$this->integration][$this->trigger] = [
            'label' => __("A user's connection request is accepted", 'dollie'),
            'action' => $this->trigger,
            'common_action' => 'friends_friendship_accepted',
            'function' => [$this, 'trigger_listener'],
            'priority' => 10,
            'accepted_args' => 4,
        ];

        return $triggers;
    }

    /**
     * Trigger listener
     *
     * @param int    $id ID.
     * @param int    $initiator_user_id Initiator User ID.
     * @param int    $friend_user_id Friendship User ID.
     * @param object $friendship Friendship.
     * @return void
     */
    public function trigger_listener($id, $initiator_user_id, $friend_user_id, $friendship)
    {

        $context['initiator'] = WordPress::get_user_context($initiator_user_id);
        $context['friend'] = WordPress::get_user_context($friend_user_id);

        AutomationController::dollie_trigger_handle_trigger(
            [
                'trigger' => $this->trigger,
                'wp_user_id' => $initiator_user_id,
                'context' => $context,
            ]
        );
    }
}

/**
 * Ignore false positive
 *
 * @psalm-suppress UndefinedMethod
 */
UserConnectionRequestAccepted::get_instance();
