-- name: ExamPrepCreateCatalogCourse :one
INSERT INTO exam_prep.catalog_courses (name, description, category, thumbnail, sort_order, publish_status)
SELECT
    $1,
    $2,
    $3,
    $4,
    coalesce((
        SELECT
            max(c.sort_order)
        FROM exam_prep.catalog_courses AS c), 0) + 1,
    $5
RETURNING
    *;

-- name: ExamPrepGetCatalogCourseByID :one
SELECT
    c.*,
    EXISTS (
        SELECT 1
        FROM exam_prep.lesson_practices p
        INNER JOIN exam_prep.unit_module_lessons l ON l.id = p.unit_module_lesson_id
        INNER JOIN exam_prep.unit_modules m ON m.id = l.unit_module_id
        INNER JOIN exam_prep.units u ON u.id = m.unit_id
        WHERE u.catalog_course_id = c.id
          AND p.publish_status = 'PUBLISHED'
          AND l.publish_status = 'PUBLISHED'
          AND m.publish_status = 'PUBLISHED'
          AND u.publish_status = 'PUBLISHED'
    ) AS has_practice
FROM exam_prep.catalog_courses c
WHERE c.id = $1;

-- name: ExamPrepListCatalogCourses :many
WITH catalog_course_counts AS (
    SELECT
        u.catalog_course_id,
        COUNT(DISTINCT u.id)::BIGINT AS units_count,
        COUNT(DISTINCT m.id)::BIGINT AS modules_count,
        COUNT(DISTINCT l.id)::BIGINT AS lessons_count
    FROM exam_prep.units u
    LEFT JOIN exam_prep.unit_modules m ON m.unit_id = u.id
        AND m.publish_status = 'PUBLISHED'
    LEFT JOIN exam_prep.unit_module_lessons l ON l.unit_module_id = m.id
        AND l.publish_status = 'PUBLISHED'
    WHERE u.publish_status = 'PUBLISHED'
    GROUP BY u.catalog_course_id
)
SELECT
    COUNT(*) OVER () AS total_count,
    c.id,
    c.name,
    c.description,
    c.category,
    c.thumbnail,
    c.sort_order,
    c.publish_status,
    COALESCE(cc.units_count, 0)::BIGINT AS units_count,
    COALESCE(cc.modules_count, 0)::BIGINT AS modules_count,
    COALESCE(cc.lessons_count, 0)::BIGINT AS lessons_count,
    EXISTS (
        SELECT 1
        FROM exam_prep.lesson_practices p
        INNER JOIN exam_prep.unit_module_lessons l ON l.id = p.unit_module_lesson_id
        INNER JOIN exam_prep.unit_modules m ON m.id = l.unit_module_id
        INNER JOIN exam_prep.units u ON u.id = m.unit_id
        WHERE u.catalog_course_id = c.id
          AND p.publish_status = 'PUBLISHED'
          AND l.publish_status = 'PUBLISHED'
          AND m.publish_status = 'PUBLISHED'
          AND u.publish_status = 'PUBLISHED'
    ) AS has_practice,
    c.created_at,
    c.updated_at
FROM exam_prep.catalog_courses c
LEFT JOIN catalog_course_counts cc ON cc.catalog_course_id = c.id
WHERE (
    sqlc.arg('published_only')::boolean = FALSE
    OR c.publish_status = 'PUBLISHED'::TEXT
)
ORDER BY c.sort_order ASC, c.id ASC
LIMIT $1 OFFSET $2;

-- name: ExamPrepListAllCatalogCourseIDs :many
SELECT
    id
FROM exam_prep.catalog_courses
ORDER BY id;

-- name: ExamPrepUpdateCatalogCourse :one
UPDATE exam_prep.catalog_courses
SET
    name = coalesce(sqlc.narg('name')::varchar, name),
    description = coalesce(sqlc.narg('description')::text, description),
    category = coalesce(sqlc.narg('category')::varchar, category),
    thumbnail = coalesce(sqlc.narg('thumbnail')::text, thumbnail),
    sort_order = coalesce(sqlc.narg('sort_order')::int, sort_order),
    publish_status = coalesce(sqlc.narg('publish_status')::varchar, publish_status),
    updated_at = CURRENT_TIMESTAMP
WHERE id = sqlc.arg('id')
RETURNING
    *;

-- name: ExamPrepDeleteCatalogCourse :exec
DELETE FROM exam_prep.catalog_courses
WHERE id = $1;
