<?php 
/**
 * full mock test - 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_id               = get_query_var('quiz_id');


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

// main oauth class
if( !class_exists('ST_ProQyz_Oauth') ) { 
    $proqyz_response = 'ProQyz OAuth class missing';
    include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/404.php';
    exit; 
}

# course id 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 id 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; 
}


if (strpos($current_url, '/simulation/general-guidance') !== false || strpos($current_url, '/simulation/result') !== false) {
    
    # begin:: proqyz-course  
    $where_params_course = [
        "where" => (object) [],
        "grant_type" => "request",
        "permission" => "course"
    ];

    $main_params_course = array_merge($where_params_course,[
        "request_id" => date("U")
    ]);

    $rest_course = (object) ST_ProQyz_Oauth::rest_post_request("/courses/course/".$proqyz_course_id, $main_params_course);
    if( isset($rest_course->success) ) {
        $remote_c_res  = (object) $rest_course->success;
        $remote_c_json = isset($remote_c_res->json)? (object) $remote_c_res->json : null;
        
        if( !$remote_c_json ){
            // remote - json
            $proqyz_response = "Invalid Response from Server";
            include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/400.php';
            exit;
        }

        $c = isset($remote_c_json->_c)? (object) $remote_c_json->_c : null;
        if( !$c ){
            // null - c
            // remote - json
            $proqyz_response = "Null Response from Server";
            include_once ST_PROQYZ_TEMPLATE_DIR . '/errors/400.php';
            exit;
        }

        $res_course         = isset($c->course)? (object) $c->course : null;
        $res_lessons        = isset($c->lessons)? (array) $c->lessons : [];
        $filtered_lesson    = get_lessons_by_id($res_lessons, $proqyz_lesson_id);

        // Reindex
        if( !$filtered_lesson ){
            // lessons
            echo "Warning: no lessons found";
        }

        $is_full_test = false;
        $lesson = (object) $filtered_lesson;

        if( strpos($current_url, '/simulation/result') !== false ){
            include_once ST_PROQYZ_TEMPLATE_DIR . '/public/full-mock-test/proqyz-fullmock-test-result-frontend.php';
        } else {
            // REMINDER
            if( isset($lesson->fullTest) && $lesson->fullTest == true ){
            
                include_once ST_PROQYZ_TEMPLATE_DIR . '/public/full-mock-test/proqyz-fullmock-frontend.php';
            } else {
                echo "this lesson is not full mock test";
            }
        }

        

    }

    if( isset($rest_course->error) ) {
        $error = (object) $rest_course->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;
    }

    // end::valiate full mock test
} else if( strpos($current_url, '/simulation/quiz') !== false ){
    include_once ST_PROQYZ_TEMPLATE_DIR . '/public/full-mock-test/proqyz-fullmock-test-frontend.php';
} else if( strpos($current_url, '/simulation/result-analyize') !== false ){
    die('result analyize');
}