<?php

namespace WPD_Platform\Integrations\Memberpress\Actions;

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

#[Action(
    id: 'mp_remove_membership',
    label: 'Remove from Membership',
    since: '1.0.0'
)]
/**
 * RemoveMembership.
 * php version 5.6
 *
 * @category RemoveMembership
 * @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
 */
/**
 * RemoveMembership
 *
 * @category RemoveMembership
 * @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 RemoveMembership extends AutomateAction
{
    use SingletonLoader;

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

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

    /**
     * Register a action.
     *
     * @param array $actions actions.
     * @return array
     */
    public function register($actions)
    {
        $actions[$this->integration][$this->action] = [
            'label' => __('Remove from Membership', '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.
     * @throws Exception Throws exception.
     *
     * @return array|bool
     */
    public function _action_listener($user_id, $automation_id, $fields, $selected_options)
    {
        if (! class_exists('MeprSubscription') || ! class_exists('MeprHooks') || ! class_exists('MeprTransaction')) {
            return [
                'status' => 'error',
                'message' => 'MemberPress classes not found.',
            ];
        }
        if (! $user_id) {
            return [
                'status' => 'error',
                'message' => 'User not found with this email address.',
            ];
        }

        if (is_array($selected_options['memberpressproduct'])) {
            $membership_id = $selected_options['memberpressproduct']['value'];
        } else {
            $membership_id = $selected_options['memberpressproduct'];
        }
        $user_obj = get_user_by('id', $user_id);
        if (! $user_obj instanceof \WP_User) {
            return [
                'status' => 'error',
                'message' => 'User not found.',
            ];
        }
        $table = MeprSubscription::account_subscr_table(
            'created_at',
            'DESC',
            '',
            '',
            'any',
            '',
            false,
            [
                'member' => $user_obj->user_login,
                'statuses' => [
                    MeprSubscription::$active_str,
                    MeprSubscription::$suspended_str,
                    MeprSubscription::$cancelled_str,
                ],
            ],
            MeprHooks::apply_filters('mepr_user_subscriptions_query_cols', ['id', 'product_id', 'created_at'])
        );

        if (0 === $table['count']) {
            return [
                'status' => 'error',
                'message' => __('Empty subscription table ', 'dollie'),

            ];
        }

        foreach ($table['results'] as $row) {
            if ($row->product_id === $membership_id || '-1' === $membership_id) {
                $sub = null;
                if ('subscription' === $row->sub_type) {
                    $sub = new MeprSubscription($row->id);
                } elseif ('transaction' === $row->sub_type) {
                    $sub = new MeprTransaction($row->id);
                }
                if ($sub) {
                    $sub->destroy();
                    $member = $sub->user();
                    $member->update_member_data();
                }
            }
        }

        $context = [];
        $context['user_email'] = $selected_options['wp_user_email'];

        if ('-1' !== $membership_id) {
            $context['membership_id'] = $membership_id;
            $context['membership_title'] = get_the_title($membership_id);
            $context['membership_url'] = get_permalink($membership_id);
            $context['membership_featured_image_id'] = get_post_meta($membership_id, '_thumbnail_id', true);
            $context['membership_featured_image_url'] = get_the_post_thumbnail_url($membership_id);
        }

        return $context;
    }
}

RemoveMembership::get_instance();
