<?php 
/**
 * v2.1.0 
 * Single Quiz shortcode
 * status: completed
 */


add_shortcode('proqyz-quiz', 'shortcode_proqyz_single_quiz');
function shortcode_proqyz_single_quiz( $atts = [], $content = null, $tag = '' ) {
    global $wpdb, $table_proqyz_v3_progress_submissions, $table_proqyz_v3_progress_submissions_user_response;

    // load header
    $atts = array_change_key_case((array)$atts, CASE_LOWER);
    # override default attributes with user attributes
    $wporg_atts = shortcode_atts(
        array(
            'quiz_title'        => null,
            'quiz_slug'         => null,
            'quiz_category'     => null,
            'quiz_type'         => null,
            'quiz_id'           => null,
            'quiz_status'       => null, 
            'created_at'        => null, 
            'quiz_icon_url'     => null, 
            'quiz_description'  => null
        ) , $atts, $tag
    );

    $quiz_category = "";
    if($wporg_atts['quiz_type'] == null){ return "Quiz type not set"; }
    if($wporg_atts['quiz_category'] == null){ return "Quiz category not set"; }

    if($wporg_atts['quiz_type'] == "ielts" && $wporg_atts['quiz_category'] == "reading"){
        $quiz_category = "ielts-reading";
    } else if($wporg_atts['quiz_type'] == "ielts" && $wporg_atts['quiz_category'] == "listening"){
        $quiz_category = "ielts-listening";
    } else if($wporg_atts['quiz_type'] == "ielts" && $wporg_atts['quiz_category'] == "writing"){
        $quiz_category = "ielts-writing";
    } else if($wporg_atts['quiz_type'] == "ielts" && $wporg_atts['quiz_category'] == "speaking"){
        $quiz_category = "ielts-speaking";
    } else if($wporg_atts['quiz_type'] == "gov" && $wporg_atts['quiz_category'] == "non-sectional-timing"){
        $quiz_category = "gov-non-sectional-timing";
    } else if($wporg_atts['quiz_type'] == "gov" && $wporg_atts['quiz_category'] == "sectional-timing"){
        $quiz_category = "gov-sectional-timing";
    } else {
        $quiz_category = "uncategorized";
    }

    if($wporg_atts['quiz_id'] !== null) {
        
        # quiz_id for rest_api
        # verify - rest api id exists
        if( 1 == 1 ) {
            # get layout for single quiz
            require_once ST_PROQYZ_LIBS_DIR . '/lms/class-progress.php'; 
            if( class_exists('Progress') ) {
                $session_obj = (object) [
                    "session_category"  => "single",
                    "quiz_category"     => $quiz_category,
                    "api_id"            => $wporg_atts['quiz_id']
                ];
    
                # check for resume details
                $progress_report = new Progress((object) $session_obj);
                $session_report  = (object) $progress_report->check_session();
                $session_details = (object) $progress_report->get_session();
    
                # logic defined here
                $resume         = false;
                $user_data      = null;
                $result         = false;
                $seconds        = 0;
    
                if( $session_report !== null && $session_details !== null ) {
                    if(isset($session_report->status)) {
                        $session_type       = isset($session_report->session_type)? $session_report->session_type : null;
                        $session_category   = isset($session_report->session_category)? $session_report->session_category : null;
                        $session_logic      = isset($session_report->logic)? $session_report->logic : null;
                        $session_id         = isset($session_report->resume_id)? $session_report->resume_id : null;

                        if($session_id !== null){
                            $resume = true;
                        }
    
                        # check session logic , if its for resume or fresh
                        if( $session_logic == "update" ) {
                            // $get_resume_session = $wpdb->get_row("SELECT `meta_value` FROM `$table_proqyz_v3_progress_submissions_meta` WHERE `submission_id` = '$session_id' AND `meta_key` = 'json' LIMIT 1");
                            $get_resume_session = $wpdb->get_row("SELECT `response_data` FROM `$table_proqyz_v3_progress_submissions_user_response` WHERE `submission_id` = '$session_id' LIMIT 1");
                            if( $get_resume_session ) {
                                $user_response = $get_resume_session->response_data;
                                $user_response = (object) maybe_unserialize($user_response);
                                if( $user_response ) {
                                    $resume = true;
                                    $user_data = $user_response;
                                    $seconds = $user_data->quiz->diff;
                                }
                            }
                        }
                    }
                }

                // --- CHANGED: Using the helper function to build the URL dynamically ---
                # update - slug
                $ilms_attempt_url = st_proqyz_get_quiz_url(
                    $quiz_category, 
                    $wporg_atts['quiz_slug'], 
                    $wporg_atts['quiz_id'], 
                    'start'
                );
                // --- END OF CHANGE ---

                ob_start(); ?>
                <a href="<?php echo esc_url($ilms_attempt_url); ?>" target="_blank">
                    <?php if( $resume ) { echo "Resume"; } ?>
                    <?php if( !$resume ) { echo "Take Test"; } ?>
                </a>
                <?php return ob_get_clean();
    
            }
        }
    }

}