<?php

namespace WPD_Platform\Integrations\Lifterlms\Actions;

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

#[Action(
    id: 'lms_mark_course_complete',
    label: 'Mark course complete for User',
    since: '1.0.0'
)]
/**
 * MarkCourseComplete.
 * php version 5.6
 *
 * @category MarkCourseComplete
 * @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
 */
/**
 * MarkCourseComplete
 *
 * @category MarkCourseComplete
 * @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 MarkCourseComplete extends AutomateAction
{
    use SingletonLoader;

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

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

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

        $course_id = isset($selected_options['course']) ? $selected_options['course'] : '0';
        $user_id = $selected_options['wp_user_email'];

        if (! class_exists('LLMS_Course') || ! class_exists('LLMS_Section')) {
            return;
        }

        if (is_email($user_id)) {
            $user = get_user_by('email', $user_id);

            if ($user) {
                $user_id = $user->ID;
                $course = get_post((int) $course_id);
                if (! $course) {
                    return [
                        'status' => 'error',
                        'message' => __('No course is available ', 'dollie'),

                    ];
                }

                if (! function_exists('llms_mark_complete')) {
                    return [
                        'status' => 'error',
                        'message' => __('The function llms_mark_complete does not exist', 'dollie'),

                    ];
                }

                $course = new \LLMS_Course($course_id);
                $sections = $course->get_sections();
                if (! empty($sections)) {
                    foreach ($sections as $section) {
                        $section = new \LLMS_Section($section->id);
                        $lessons = $section->get_lessons();
                        if (! empty($lessons)) {
                            foreach ($lessons as $lesson) {
                                llms_mark_complete($user_id, $lesson->id, 'lesson');
                            }
                        }
                        llms_mark_complete($user_id, $section->id, 'section');
                    }
                }
                llms_mark_complete($user_id, $course_id, 'course');

                return LifterLMS::get_lms_course_context($course_id);
            } else {
                return [
                    'status' => 'error',
                    'message' => 'User not exists.',
                ];
            }
        } else {
            return [
                'status' => 'error',
                'message' => 'Enter valid email address.',
            ];
        }
    }
}

MarkCourseComplete::get_instance();
