-- name: AddQuestionToSet :one
INSERT INTO question_set_items (
    set_id,
    question_id,
    display_order
)
VALUES ($1, $2, COALESCE($3, 0))
ON CONFLICT (set_id, question_id) DO UPDATE SET display_order = EXCLUDED.display_order
RETURNING *;

-- name: GetQuestionSetItems :many
SELECT 
    qsi.id,
    qsi.set_id,
    qsi.question_id,
    qsi.display_order,
    q.question_text,
    q.question_type,
    q.dynamic_payload,
    q.difficulty_level,
    q.points,
    q.explanation,
    q.tips,
    q.voice_prompt,
    q.sample_answer_voice_prompt,
    q.image_url,
    q.status as question_status,
    qaa.correct_answer_text AS audio_correct_answer_text
FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id
LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id
WHERE qsi.set_id = $1
  AND q.status != 'ARCHIVED'
ORDER BY qsi.display_order;

-- name: GetQuestionSetItemsPaginated :many
SELECT
    COUNT(*) OVER () AS total_count,
    qsi.id,
    qsi.set_id,
    qsi.question_id,
    qsi.display_order,
    q.question_text,
    q.question_type,
    q.dynamic_payload,
    q.difficulty_level,
    q.points,
    q.explanation,
    q.tips,
    q.voice_prompt,
    q.sample_answer_voice_prompt,
    q.image_url,
    q.status AS question_status,
    qaa.correct_answer_text AS audio_correct_answer_text
FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id
LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id
WHERE qsi.set_id = $1
  AND ($2::VARCHAR IS NULL OR $2 = '' OR q.question_type = $2)
  AND q.status != 'ARCHIVED'
ORDER BY qsi.display_order
LIMIT sqlc.narg('limit')::INT
OFFSET sqlc.narg('offset')::INT;

-- name: GetPublishedQuestionsInSet :many
SELECT 
    qsi.id,
    qsi.set_id,
    qsi.question_id,
    qsi.display_order,
    q.question_text,
    q.question_type,
    q.dynamic_payload,
    q.difficulty_level,
    q.points,
    q.explanation,
    q.tips,
    q.voice_prompt,
    q.sample_answer_voice_prompt,
    q.image_url,
    qaa.correct_answer_text AS audio_correct_answer_text
FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id
LEFT JOIN question_audio_answers qaa ON qaa.question_id = q.id
WHERE qsi.set_id = $1
  AND q.status = 'PUBLISHED'
ORDER BY qsi.display_order;

-- name: RemoveQuestionFromSet :exec
DELETE FROM question_set_items
WHERE set_id = $1 AND question_id = $2;

-- name: UpdateQuestionOrder :exec
UPDATE question_set_items
SET display_order = $1
WHERE set_id = $2 AND question_id = $3;

-- name: CountQuestionsInSet :one
SELECT COUNT(*) as count
FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id
WHERE qsi.set_id = $1
  AND q.status != 'ARCHIVED';

-- name: GetQuestionTypeCountsInSet :many
SELECT
    q.question_type,
    COUNT(*)::bigint AS question_count
FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id
WHERE qsi.set_id = $1
  AND q.status != 'ARCHIVED'
GROUP BY q.question_type
ORDER BY q.question_type;

-- name: GetQuestionSetsContainingQuestion :many
SELECT qs.*
FROM question_sets qs
JOIN question_set_items qsi ON qsi.set_id = qs.id
WHERE qsi.question_id = $1
  AND qs.status != 'ARCHIVED';
