-- name: ExamPrepCreateUnitModule :one
INSERT INTO exam_prep.unit_modules (unit_id, name, description, thumbnail, icon, sort_order, publish_status)
SELECT
    $1,
    $2,
    $3,
    $4,
    $5,
    coalesce((
        SELECT
            max(m.sort_order)
        FROM exam_prep.unit_modules m
        WHERE
            m.unit_id = $1), 0) + 1,
    $6
RETURNING
    *;

-- name: ExamPrepGetUnitModuleByID :one
SELECT
    m.*,
    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
        WHERE l.unit_module_id = m.id
          AND p.publish_status = 'PUBLISHED'
          AND l.publish_status = 'PUBLISHED'
    ) AS has_practice
FROM exam_prep.unit_modules m
WHERE m.id = $1;

-- name: ExamPrepListUnitModuleIDsByUnit :many
SELECT
    m.id
FROM exam_prep.unit_modules m
WHERE
    m.unit_id = $1
ORDER BY
    m.id;

-- Published modules only, for learner-facing progress rollups.
-- name: ExamPrepListPublishedUnitModuleIDsByUnit :many
SELECT
    m.id
FROM exam_prep.unit_modules m
WHERE
    m.unit_id = $1
    AND m.publish_status = 'PUBLISHED'
ORDER BY
    m.id;

-- name: ExamPrepListUnitModulesByUnit :many
WITH module_counts AS (
    SELECT
        m.id AS module_id,
        COUNT(DISTINCT l.id)::BIGINT AS lessons_count,
        COUNT(DISTINCT p.id)::BIGINT AS practices_count
    FROM exam_prep.unit_modules m
    LEFT JOIN exam_prep.unit_module_lessons l ON l.unit_module_id = m.id
        AND l.publish_status = 'PUBLISHED'
    LEFT JOIN exam_prep.lesson_practices p ON p.unit_module_lesson_id = l.id
        AND p.publish_status = 'PUBLISHED'
    GROUP BY m.id
)
SELECT
    COUNT(*) OVER () AS total_count,
    m.id,
    m.unit_id,
    m.name,
    m.description,
    m.thumbnail,
    m.icon,
    m.sort_order,
    m.publish_status,
    COALESCE(mc.lessons_count, 0)::BIGINT AS lessons_count,
    COALESCE(mc.practices_count, 0)::BIGINT AS practices_count,
    (COALESCE(mc.practices_count, 0)::BIGINT > 0) AS has_practice,
    m.created_at,
    m.updated_at
FROM exam_prep.unit_modules m
LEFT JOIN module_counts mc ON mc.module_id = m.id
WHERE
    m.unit_id = $1
    AND (
        sqlc.arg('published_only')::boolean = FALSE
        OR m.publish_status = 'PUBLISHED'::TEXT
    )
ORDER BY
    m.sort_order ASC,
    m.id ASC
LIMIT $2
OFFSET $3;

-- name: ExamPrepUpdateUnitModule :one
UPDATE exam_prep.unit_modules
SET
    name = coalesce(sqlc.narg('name')::varchar, name),
    description = coalesce(sqlc.narg('description')::text, description),
    thumbnail = coalesce(sqlc.narg('thumbnail')::text, thumbnail),
    icon = coalesce(sqlc.narg('icon')::text, icon),
    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: ExamPrepDeleteUnitModule :exec
DELETE FROM exam_prep.unit_modules
WHERE id = $1;
