<?php 
// These functions are available globally for use in your templates.

/**
 * Generates the full URL for a specific ProQyz course page.
 *
 * @param int|string $course_id The ID of the course.
 * @return string The full, permalink-friendly URL.
 */
function st_proqyz_get_course_url( $course_id ) {
    $base   = get_option('proqyz_permalink_base', 'proqyz');
    $course = get_option('proqyz_permalink_course', 'proqyz-course');

    return home_url( '/' . $base . '/' . $course . '/' . $course_id . '/' );
}

/**
 * Generates the full URL for a standalone quiz page.
 *
 * @param string $category The quiz category slug.
 * @param string $slug The quiz slug.
 * @param int|string $quiz_id The ID of the quiz.
 * @param string $action Optional. The action to perform (e.g., 'start').
 * @return string The full, permalink-friendly URL.
 */
function st_proqyz_get_quiz_url( $category, $slug, $quiz_id, $action = '' ) {
    $base = get_option('proqyz_permalink_base', 'proqyz');
    $url  = home_url( '/' . $base . '/quiz/' . $category . '/' . $slug . '/' . $quiz_id . '/' );

    if ( ! empty( $action ) ) {
        $url .= $action . '/';
    }
    return $url;
}

/**
 * Generates the full URL for a specific quiz within a course.
 * It intelligently selects the correct URL slug based on the course type.
 *
 * @param array $args An array of required URL parts.
 * @return string The full, permalink-friendly URL.
 */
function st_proqyz_get_course_quiz_url( $args = [] ) {
    $defaults = [
        'course_type'   => 'proqyz-course', // Defaults to your course type
        'course_id'     => '',
        'lesson_id'     => '',
        'quiz_category' => '',
        'quiz_slug'     => '',
        'quiz_id'       => '',
        'action'        => 'start',
    ];

    $params = wp_parse_args($args, $defaults);
    $base   = get_option('proqyz_permalink_base', 'proqyz');
    
    // --- Smart Slug Selection ---
    $type_slug = '';
    if ( $params['course_type'] === 'sfwd-course' ) {
        // For LearnDash, always use the static 'sfwd-course' slug.
        $type_slug = 'sfwd-course';
    } else {
        // For your own courses, use the customizable slug from the settings.
        $type_slug = get_option('proqyz_permalink_course', 'proqyz-course');
    }
    // --- End Smart Slug Selection ---

    // Build the URL from the parts
    $url_parts = [
        $base,
        'course',
        $type_slug, // Use the correctly selected slug here
        $params['course_id'],
        $params['lesson_id'],
        $params['quiz_category'],
        $params['quiz_slug'],
        $params['quiz_id'],
        $params['action']
    ];

    // Remove any empty parts and join with slashes
    $url_path = implode('/', array_filter($url_parts));

    return home_url( '/' . $url_path . '/' );
}

/**
 * Generates the URL for a full mock test page (guidance or result).
 *
 * @param int|string $course_id The ID of the course.
 * @param int|string $lesson_id The ID of the lesson containing the test.
 * @param string     $action    The specific page ('general-guidance' or 'result').
 * @return string The full, permalink-friendly URL.
 */
function st_proqyz_get_mock_test_url( $course_id, $lesson_id, $action = 'general-guidance' ) {
    $base   = get_option('proqyz_permalink_base', 'proqyz');
    $course = get_option('proqyz_permalink_course', 'proqyz-course');

    // Build the URL from the parts
    $url_path = $base . '/' . $course . '/' . $course_id . '/' . $lesson_id . '/simulation/' . $action;

    return home_url( '/' . $url_path );
}

/**
 * Generates the URL for a quiz result page.
 *
 * @param string $quiz_category The quiz category slug.
 * @param string $quiz_slug     The quiz slug.
 * @param int|string $quiz_id       The ID of the quiz.
 * @param int|string $result_id     The ID of the result.
 * @return string The full, permalink-friendly URL.
 */
function st_proqyz_get_result_url( $quiz_category, $quiz_slug, $quiz_id, $result_id ) {
    $base = get_option('proqyz_permalink_base', 'proqyz');

    // Build the URL from the parts
    $url_path = $base . '/result/' . $quiz_category . '/' . $quiz_slug . '/' . $quiz_id . '/' . $result_id;

    return home_url( '/' . $url_path . '/' );
}

function st_proqyz_get_result_analyze_url( $quiz_category, $quiz_slug, $quiz_id, $result_id ) {
    $base = get_option('proqyz_permalink_base', 'proqyz');

    // Build the URL from the parts
    $url_path = $base . '/result-analyize/' . $quiz_category . '/' . $quiz_slug . '/' . $quiz_id . '/' . $result_id;

    return home_url( '/' . $url_path . '/' );
}

/**
 * Generates the URL for a quiz review page.
 *
 * @param string     $quiz_category The quiz category slug.
 * @param string     $quiz_slug     The quiz slug.
 * @param int|string $quiz_id       The ID of the quiz.
 * @param int|string $result_id     The ID of the result to review.
 * @return string The full, permalink-friendly URL.
 */
function st_proqyz_get_review_url( $quiz_category, $quiz_slug, $quiz_id, $result_id ) {
    $base = get_option('proqyz_permalink_base', 'proqyz');

    // Build the URL from the parts
    $url_path = $base . '/review/' . $quiz_category . '/' . $quiz_slug . '/' . $quiz_id . '/' . $result_id;

    return home_url( '/' . $url_path . '/' );
}

/**
 * Generates the URL for a specific quiz within a full mock test.
 *
 * @param int|string $course_id     The ID of the course.
 * @param int|string $lesson_id     The ID of the lesson.
 * @param string     $quiz_category The combined quiz category (e.g., 'ielts-reading').
 * @param int|string $quiz_id       The ID of the quiz.
 * @return string The full, permalink-friendly URL.
 */
function st_proqyz_get_mock_test_quiz_url( $course_id, $lesson_id, $quiz_category, $quiz_id ) {
    $base   = get_option('proqyz_permalink_base', 'proqyz');
    $course = get_option('proqyz_permalink_course', 'proqyz-course');

    // Build the URL from the parts
    $url_path = $base . '/' . $course . '/' . $course_id . '/' . $lesson_id . '/simulation/quiz/' . $quiz_category . '/' . $quiz_id;

    return home_url( '/' . $url_path . '/' );
}


function st_proqyz_get_fullmock_analyize_url( $course_id, $lesson_id, $step, $result_id ) {
    $base = get_option('proqyz_permalink_base', 'proqyz');
    $course = get_option('proqyz_permalink_course', 'proqyz-course');
    // Build the URL from the parts
    $url_path = $base . '/' . $course . '/' . $course_id . '/' . $lesson_id . '/simulation/result-analyize/' . $step . '/' . $result_id;

    return home_url( '/' . $url_path . '/' );
}