<?php

namespace WPD_Platform\Integrations\Tutorlms\Actions;

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

#[Action(
    id: 'tlms_reset_user_course_progress',
    label: 'Reset User Course Progress',
    since: '1.0.0'
)]
/**
 * ResetUserCourseProgress.
 * php version 5.6
 *
 * @category ResetUserCourseProgress
 * @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
 */
/**
 * ResetUserCourseProgress
 *
 * @category ResetUserCourseProgress
 * @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 ResetUserCourseProgress extends AutomateAction
{
    use SingletonLoader;

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

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

    /**
     * Register a action.
     *
     * @param array $actions actions.
     * @return array
     */
    public function register($actions)
    {
        $actions[$this->integration][$this->action] = [
            'label' => __('Reset User Course Progress', '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.
     * @return array|void
     * @throws Exception Exception.
     */
    public function _action_listener($user_id, $automation_id, $fields, $selected_options)
    {

        $course_id = isset($selected_options['courses']) ? $selected_options['courses'] : '0';
        $user_email = (isset($selected_options['wp_user_email'])) ? $selected_options['wp_user_email'] : '';

        if (! function_exists('tutor_utils')) {
            return [];
        }
        if (is_email($user_email)) {
            $user = get_user_by('email', $user_email);
            if ($user) {
                $user_id = $user->ID;

                if (! $course_id || ! is_numeric($course_id) || ! tutor_utils()->is_enrolled($course_id, $user_id)) {
                    return [
                        'status' => 'error',
                        'message' => 'Invalid Course ID or Access Denied.',
                    ];
                }
                tutor_utils()->delete_course_progress($course_id, $user_id);
                $response = [
                    'status' => esc_attr__('Success', 'dollie'),
                    'response' => esc_attr__('User course progress has been successfully reset.', 'dollie'),

                ];

                return $response;
            } else {
                return [
                    'status' => 'error',
                    'message' => 'User not found.',
                ];
            }
        } else {
            return [
                'status' => 'error',
                'message' => 'Enter valid email address.',
            ];
        }
    }
}

ResetUserCourseProgress::get_instance();
