// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: question_set_items.sql

package dbgen

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const AddQuestionToSet = `-- 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 id, set_id, question_id, display_order, created_at
`

type AddQuestionToSetParams struct {
	SetID      int64       `json:"set_id"`
	QuestionID int64       `json:"question_id"`
	Column3    interface{} `json:"column_3"`
}

func (q *Queries) AddQuestionToSet(ctx context.Context, arg AddQuestionToSetParams) (QuestionSetItem, error) {
	row := q.db.QueryRow(ctx, AddQuestionToSet, arg.SetID, arg.QuestionID, arg.Column3)
	var i QuestionSetItem
	err := row.Scan(
		&i.ID,
		&i.SetID,
		&i.QuestionID,
		&i.DisplayOrder,
		&i.CreatedAt,
	)
	return i, err
}

const CountQuestionsInSet = `-- 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'
`

func (q *Queries) CountQuestionsInSet(ctx context.Context, setID int64) (int64, error) {
	row := q.db.QueryRow(ctx, CountQuestionsInSet, setID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const GetPublishedQuestionsInSet = `-- name: GetPublishedQuestionsInSet :many
SELECT 
    qsi.id,
    qsi.set_id,
    qsi.question_id,
    qsi.display_order,
    q.question_text,
    q.question_type,
    q.difficulty_level,
    q.points,
    q.explanation,
    q.tips,
    q.voice_prompt
FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id
WHERE qsi.set_id = $1
  AND q.status = 'PUBLISHED'
ORDER BY qsi.display_order
`

type GetPublishedQuestionsInSetRow struct {
	ID              int64       `json:"id"`
	SetID           int64       `json:"set_id"`
	QuestionID      int64       `json:"question_id"`
	DisplayOrder    int32       `json:"display_order"`
	QuestionText    string      `json:"question_text"`
	QuestionType    string      `json:"question_type"`
	DifficultyLevel pgtype.Text `json:"difficulty_level"`
	Points          int32       `json:"points"`
	Explanation     pgtype.Text `json:"explanation"`
	Tips            pgtype.Text `json:"tips"`
	VoicePrompt     pgtype.Text `json:"voice_prompt"`
}

func (q *Queries) GetPublishedQuestionsInSet(ctx context.Context, setID int64) ([]GetPublishedQuestionsInSetRow, error) {
	rows, err := q.db.Query(ctx, GetPublishedQuestionsInSet, setID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetPublishedQuestionsInSetRow
	for rows.Next() {
		var i GetPublishedQuestionsInSetRow
		if err := rows.Scan(
			&i.ID,
			&i.SetID,
			&i.QuestionID,
			&i.DisplayOrder,
			&i.QuestionText,
			&i.QuestionType,
			&i.DifficultyLevel,
			&i.Points,
			&i.Explanation,
			&i.Tips,
			&i.VoicePrompt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetQuestionSetItems = `-- name: GetQuestionSetItems :many
SELECT 
    qsi.id,
    qsi.set_id,
    qsi.question_id,
    qsi.display_order,
    q.question_text,
    q.question_type,
    q.difficulty_level,
    q.points,
    q.explanation,
    q.tips,
    q.voice_prompt,
    q.status as question_status
FROM question_set_items qsi
JOIN questions q ON q.id = qsi.question_id
WHERE qsi.set_id = $1
  AND q.status != 'ARCHIVED'
ORDER BY qsi.display_order
`

type GetQuestionSetItemsRow struct {
	ID              int64       `json:"id"`
	SetID           int64       `json:"set_id"`
	QuestionID      int64       `json:"question_id"`
	DisplayOrder    int32       `json:"display_order"`
	QuestionText    string      `json:"question_text"`
	QuestionType    string      `json:"question_type"`
	DifficultyLevel pgtype.Text `json:"difficulty_level"`
	Points          int32       `json:"points"`
	Explanation     pgtype.Text `json:"explanation"`
	Tips            pgtype.Text `json:"tips"`
	VoicePrompt     pgtype.Text `json:"voice_prompt"`
	QuestionStatus  string      `json:"question_status"`
}

func (q *Queries) GetQuestionSetItems(ctx context.Context, setID int64) ([]GetQuestionSetItemsRow, error) {
	rows, err := q.db.Query(ctx, GetQuestionSetItems, setID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetQuestionSetItemsRow
	for rows.Next() {
		var i GetQuestionSetItemsRow
		if err := rows.Scan(
			&i.ID,
			&i.SetID,
			&i.QuestionID,
			&i.DisplayOrder,
			&i.QuestionText,
			&i.QuestionType,
			&i.DifficultyLevel,
			&i.Points,
			&i.Explanation,
			&i.Tips,
			&i.VoicePrompt,
			&i.QuestionStatus,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetQuestionSetsContainingQuestion = `-- name: GetQuestionSetsContainingQuestion :many
SELECT qs.id, qs.title, qs.description, qs.set_type, qs.owner_type, qs.owner_id, qs.banner_image, qs.persona, qs.time_limit_minutes, qs.passing_score, qs.shuffle_questions, qs.status, qs.created_at, qs.updated_at, qs.sub_course_video_id
FROM question_sets qs
JOIN question_set_items qsi ON qsi.set_id = qs.id
WHERE qsi.question_id = $1
  AND qs.status != 'ARCHIVED'
`

func (q *Queries) GetQuestionSetsContainingQuestion(ctx context.Context, questionID int64) ([]QuestionSet, error) {
	rows, err := q.db.Query(ctx, GetQuestionSetsContainingQuestion, questionID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []QuestionSet
	for rows.Next() {
		var i QuestionSet
		if err := rows.Scan(
			&i.ID,
			&i.Title,
			&i.Description,
			&i.SetType,
			&i.OwnerType,
			&i.OwnerID,
			&i.BannerImage,
			&i.Persona,
			&i.TimeLimitMinutes,
			&i.PassingScore,
			&i.ShuffleQuestions,
			&i.Status,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.SubCourseVideoID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

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

type RemoveQuestionFromSetParams struct {
	SetID      int64 `json:"set_id"`
	QuestionID int64 `json:"question_id"`
}

func (q *Queries) RemoveQuestionFromSet(ctx context.Context, arg RemoveQuestionFromSetParams) error {
	_, err := q.db.Exec(ctx, RemoveQuestionFromSet, arg.SetID, arg.QuestionID)
	return err
}

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

type UpdateQuestionOrderParams struct {
	DisplayOrder int32 `json:"display_order"`
	SetID        int64 `json:"set_id"`
	QuestionID   int64 `json:"question_id"`
}

func (q *Queries) UpdateQuestionOrder(ctx context.Context, arg UpdateQuestionOrderParams) error {
	_, err := q.db.Exec(ctx, UpdateQuestionOrder, arg.DisplayOrder, arg.SetID, arg.QuestionID)
	return err
}
