-- name: CreateLmsPractice :one
INSERT INTO lms_practices (
    course_id, module_id, lesson_id,
    title, story_description, story_image, persona_id, question_set_id, quick_tips, publish_status
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING *;

-- name: GetLmsPracticeByID :one
SELECT *
FROM lms_practices
WHERE id = $1;

-- name: GetLmsPracticeByQuestionSetID :one
SELECT *
FROM lms_practices
WHERE question_set_id = $1
ORDER BY id DESC
LIMIT 1;

-- name: ListLmsPracticesByCourseID :many
SELECT
    COUNT(*) OVER () AS total_count,
    p.id,
    p.course_id,
    p.module_id,
    p.lesson_id,
    p.title,
    p.story_description,
    p.story_image,
    p.persona_id,
    p.question_set_id,
    p.quick_tips,
    p.publish_status,
    p.created_at,
    p.updated_at
FROM lms_practices p
INNER JOIN question_sets qs ON qs.id = p.question_set_id
WHERE p.course_id = $1
  AND (
    sqlc.arg('published_only')::boolean = FALSE
          OR (
            p.publish_status = 'PUBLISHED'::TEXT
            AND qs.set_type = 'PRACTICE'
            AND qs.status = 'PUBLISHED'
          )
      )
ORDER BY p.created_at DESC
LIMIT $2 OFFSET $3;

-- name: ListLmsPracticesByModuleID :many
SELECT
    COUNT(*) OVER () AS total_count,
    p.id,
    p.course_id,
    p.module_id,
    p.lesson_id,
    p.title,
    p.story_description,
    p.story_image,
    p.persona_id,
    p.question_set_id,
    p.quick_tips,
    p.publish_status,
    p.created_at,
    p.updated_at
FROM lms_practices p
INNER JOIN question_sets qs ON qs.id = p.question_set_id
WHERE p.module_id = $1
  AND (
    sqlc.arg('published_only')::boolean = FALSE
          OR (
            p.publish_status = 'PUBLISHED'::TEXT
            AND qs.set_type = 'PRACTICE'
            AND qs.status = 'PUBLISHED'
          )
      )
ORDER BY p.created_at DESC
LIMIT $2 OFFSET $3;

-- name: ListLmsPracticesByLessonID :many
SELECT
    COUNT(*) OVER () AS total_count,
    p.id,
    p.course_id,
    p.module_id,
    p.lesson_id,
    p.title,
    p.story_description,
    p.story_image,
    p.persona_id,
    p.question_set_id,
    p.quick_tips,
    p.publish_status,
    p.created_at,
    p.updated_at
FROM lms_practices p
INNER JOIN question_sets qs ON qs.id = p.question_set_id
WHERE p.lesson_id = $1
  AND (
    sqlc.arg('published_only')::boolean = FALSE
          OR (
            p.publish_status = 'PUBLISHED'::TEXT
            AND qs.set_type = 'PRACTICE'
            AND qs.status = 'PUBLISHED'
          )
      )
ORDER BY p.created_at DESC
LIMIT $2 OFFSET $3;

-- name: UpdateLmsPractice :one
UPDATE lms_practices
SET
    title = COALESCE(sqlc.narg('title')::varchar, title),
    story_description = COALESCE(sqlc.narg('story_description')::text, story_description),
    story_image = COALESCE(sqlc.narg('story_image')::text, story_image),
    persona_id = COALESCE(sqlc.narg('persona_id')::bigint, persona_id),
    question_set_id = COALESCE(sqlc.narg('question_set_id')::bigint, question_set_id),
    quick_tips = COALESCE(sqlc.narg('quick_tips')::text, quick_tips),
    publish_status = COALESCE(sqlc.narg('publish_status')::varchar, publish_status),
    updated_at = CURRENT_TIMESTAMP
WHERE id = sqlc.arg('id')
RETURNING *;

-- name: DeleteLmsPractice :exec
DELETE FROM lms_practices
WHERE id = $1;
