<?php
/**
 * template: ProQyz Course template 
 */
defined('ABSPATH') || exit;
require_once ST_PROQYZ_LIBS_DIR . '/lms/utill.php';
require_once 'functions.php';



$home_url                     = home_url();
$proqyz_course_id             = get_query_var('course_id');
$proqyz_lesson_id             = get_query_var('lesson_id');
$proqyz_quiz_category         = get_query_var('quiz_category');
$proqyz_quiz_slug             = get_query_var('quiz_slug');
$proqyz_quiz_id               = get_query_var('quiz_id');
$proqyz_action                = get_query_var('action');

// Get the current page URL
$current_url = home_url($_SERVER['REQUEST_URI']);


// Check if it's a quiz page
if (strpos($current_url, '/sfwd-course') !== false) {
    # course slug validation
    if(!$proqyz_course_id || empty($proqyz_course_id)){
        $proqyz_response = 'Course id missing or empty';
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
        exit; 
    }

    

    // Load WordPress header
    get_header();

    // Start the loop
    ?>
    <div class="entry-content" style="display:flex;align-items-center;width:100%;flex-direction:column;">
        <?php   
            // Include the shortcode content
            echo do_shortcode("[proqyz-course course_id='$proqyz_course_id']");
        ?>
    </div>
    <?php
    // Load WordPress footer
    get_footer();

} else {

    # course slug validation
    if(!$proqyz_course_id || empty($proqyz_course_id)){
        $proqyz_response = 'Course id missing or empty';
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
        exit; 
    }

    # lesson slug validation
    if(!$proqyz_lesson_id || empty($proqyz_lesson_id)){
        $proqyz_response = 'Lesson id missing or empty';
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
        exit; 
    }

    # check quiz category
    if (!in_array($proqyz_quiz_category, ["ielts-reading", "ielts-listening", "ielts-writing", "ielts-speaking","gov-sectional-timing","gov-non-sectional-timing"])) {
        $proqyz_response = 'Unknown Quiz category';
        include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/400.php';
        exit;
    }

    # get remote course with course id
    if( class_exists('ST_ProQyz_Oauth') ) {

        # sfwd course
        $sfwd_course        = [];
        $sfwd_course_title  = "";




        $where_params = [
            "where" => (object) [],
            "grant_type" => "request",
            "permission" => "quiz"
        ];

        $main_params = array_merge($where_params,[
            "request_id" => date("U")
        ]);


        $response = (object) ST_ProQyz_Oauth::rest_post_request("/quizzes/quiz/".$proqyz_quiz_id, $main_params);
        if (!isset($response->error)) {
            /**
            * sections [html]
            * styles   [html]
            */
    
            $html_response = load_quiz((object) [
                'category'          => $proqyz_quiz_category,
                'res'               => $response,
                'session'           => (object) [
                    "course_title"      => $sfwd_course_title,
                    "course"            => $sfwd_course,
                    "session_category"  => "course",
                    "course_id"         => $proqyz_course_id,
                    "lesson_id"         => $proqyz_lesson_id,
                    'course_category'   => "proqyz-course",
                    "quiz_category"     => $proqyz_quiz_category,
                    "api_id"            => $proqyz_quiz_id
                ]
            ]);
    
            echo $html_response;
    
        } else {
            $error = (object) $response->error;
            $proqyz_response  = $error->message;
            $proqyz_code      = $error->status;
            if($proqyz_code == "404"){
                include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
            } else {
                include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/500.php';
            }
            exit;
        }
    }
}