-- Recent activity feed: LMS completion milestones (chronological merge in application code).

-- name: ListUserLessonCompletionsRecentActivity :many
SELECT
    ulp.completed_at AS occurred_at,
    l.id AS lesson_id,
    l.title AS lesson_title,
    m.id AS module_id,
    m.name AS module_name,
    m.sort_order AS module_sort_order,
    c.id AS course_id,
    c.name AS course_name,
    p.id AS program_id,
    p.name AS program_name
FROM
    lms_user_lesson_progress ulp
    INNER JOIN lessons l ON l.id = ulp.lesson_id
    INNER JOIN modules m ON m.id = l.module_id
    INNER JOIN courses c ON c.id = m.course_id
        AND c.program_id = m.program_id
    INNER JOIN programs p ON p.id = c.program_id
WHERE
    ulp.user_id = $1;

-- name: ListUserModuleCompletionsRecentActivity :many
SELECT
    mrf.completed_at AS occurred_at,
    m.id AS module_id,
    m.name AS module_name,
    m.sort_order AS module_sort_order,
    c.id AS course_id,
    c.name AS course_name,
    p.id AS program_id,
    p.name AS program_name
FROM
    lms_user_module_progress mrf
    INNER JOIN modules m ON m.id = mrf.module_id
    INNER JOIN courses c ON c.id = m.course_id
        AND c.program_id = m.program_id
    INNER JOIN programs p ON p.id = c.program_id
WHERE
    mrf.user_id = $1;

-- name: ListUserCourseCompletionsRecentActivity :many
SELECT
    crf.completed_at AS occurred_at,
    c.id AS course_id,
    c.name AS course_name,
    p.id AS program_id,
    p.name AS program_name
FROM
    lms_user_course_progress crf
    INNER JOIN courses c ON c.id = crf.course_id
    INNER JOIN programs p ON p.id = c.program_id
WHERE
    crf.user_id = $1;

-- name: ListUserProgramCompletionsRecentActivity :many
SELECT
    prf.completed_at AS occurred_at,
    p.id AS program_id,
    p.name AS program_name
FROM
    lms_user_program_progress prf
    INNER JOIN programs p ON p.id = prf.program_id
WHERE
    prf.user_id = $1;

-- name: ListUserPracticeCompletionsRecentActivity :many
SELECT
    x.occurred_at,
    x.scope,
    x.lms_practice_id,
    x.practice_title,
    COALESCE(x.lesson_id, 0)::BIGINT AS lesson_id,
    COALESCE(x.lesson_title, '')::TEXT AS lesson_title,
    COALESCE(x.module_id, 0)::BIGINT AS module_id,
    COALESCE(x.module_name, '')::TEXT AS module_name,
    COALESCE(x.module_sort_order, 0)::INT AS module_sort_order,
    x.course_id,
    x.course_name,
    x.program_id,
    x.program_name
FROM (
    SELECT
        upp.completed_at AS occurred_at,
        'lesson'::TEXT AS scope,
        lp.id AS lms_practice_id,
        lp.title AS practice_title,
        l.id AS lesson_id,
        l.title AS lesson_title,
        m.id AS module_id,
        m.name AS module_name,
        m.sort_order AS module_sort_order,
        c.id AS course_id,
        c.name AS course_name,
        p.id AS program_id,
        p.name AS program_name
    FROM
        user_practice_progress upp
        INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
            AND lp.lesson_id IS NOT NULL
        INNER JOIN question_sets qs ON qs.id = upp.question_set_id
            AND qs.set_type = 'PRACTICE'
            AND qs.status = 'PUBLISHED'
        INNER JOIN lessons l ON l.id = lp.lesson_id
        INNER JOIN modules m ON m.id = l.module_id
        INNER JOIN courses c ON c.id = m.course_id
            AND c.program_id = m.program_id
        INNER JOIN programs p ON p.id = c.program_id
    WHERE
        upp.user_id = $1
        AND upp.completed_at IS NOT NULL
    UNION ALL
    SELECT
        upp.completed_at,
        'module'::TEXT,
        lp.id,
        lp.title,
        NULL::BIGINT,
        NULL::VARCHAR,
        m.id,
        m.name,
        m.sort_order,
        c.id,
        c.name,
        p.id,
        p.name
    FROM
        user_practice_progress upp
        INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
            AND lp.module_id IS NOT NULL
            AND lp.lesson_id IS NULL
        INNER JOIN question_sets qs ON qs.id = upp.question_set_id
            AND qs.set_type = 'PRACTICE'
            AND qs.status = 'PUBLISHED'
        INNER JOIN modules m ON m.id = lp.module_id
        INNER JOIN courses c ON c.id = m.course_id
            AND c.program_id = m.program_id
        INNER JOIN programs p ON p.id = c.program_id
    WHERE
        upp.user_id = $1
        AND upp.completed_at IS NOT NULL
    UNION ALL
    SELECT
        upp.completed_at,
        'course'::TEXT,
        lp.id,
        lp.title,
        NULL::BIGINT,
        NULL::VARCHAR,
        NULL::BIGINT,
        NULL::VARCHAR,
        NULL::INT,
        c.id,
        c.name,
        p.id,
        p.name
    FROM
        user_practice_progress upp
        INNER JOIN lms_practices lp ON lp.question_set_id = upp.question_set_id
            AND lp.course_id IS NOT NULL
            AND lp.module_id IS NULL
            AND lp.lesson_id IS NULL
        INNER JOIN question_sets qs ON qs.id = upp.question_set_id
            AND qs.set_type = 'PRACTICE'
            AND qs.status = 'PUBLISHED'
        INNER JOIN courses c ON c.id = lp.course_id
        INNER JOIN programs p ON p.id = c.program_id
    WHERE
        upp.user_id = $1
        AND upp.completed_at IS NOT NULL
) AS x;
