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

package dbgen

import (
	"context"

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

const CreateSubCourse = `-- name: CreateSubCourse :one
INSERT INTO sub_courses (
    course_id,
    title,
    description,
    thumbnail,
    display_order,
    level,
    is_active
)
VALUES ($1, $2, $3, $4, COALESCE($5, 0), $6, COALESCE($7, true))
RETURNING id, course_id, title, description, thumbnail, display_order, level, is_active
`

type CreateSubCourseParams struct {
	CourseID    int64       `json:"course_id"`
	Title       string      `json:"title"`
	Description pgtype.Text `json:"description"`
	Thumbnail   pgtype.Text `json:"thumbnail"`
	Column5     interface{} `json:"column_5"`
	Level       string      `json:"level"`
	Column7     interface{} `json:"column_7"`
}

func (q *Queries) CreateSubCourse(ctx context.Context, arg CreateSubCourseParams) (SubCourse, error) {
	row := q.db.QueryRow(ctx, CreateSubCourse,
		arg.CourseID,
		arg.Title,
		arg.Description,
		arg.Thumbnail,
		arg.Column5,
		arg.Level,
		arg.Column7,
	)
	var i SubCourse
	err := row.Scan(
		&i.ID,
		&i.CourseID,
		&i.Title,
		&i.Description,
		&i.Thumbnail,
		&i.DisplayOrder,
		&i.Level,
		&i.IsActive,
	)
	return i, err
}

const DeactivateSubCourse = `-- name: DeactivateSubCourse :exec
UPDATE sub_courses
SET is_active = FALSE
WHERE id = $1
`

func (q *Queries) DeactivateSubCourse(ctx context.Context, id int64) error {
	_, err := q.db.Exec(ctx, DeactivateSubCourse, id)
	return err
}

const DeleteSubCourse = `-- name: DeleteSubCourse :one
DELETE FROM sub_courses
WHERE id = $1
RETURNING id, course_id, title, description, thumbnail, display_order, level, is_active
`

func (q *Queries) DeleteSubCourse(ctx context.Context, id int64) (SubCourse, error) {
	row := q.db.QueryRow(ctx, DeleteSubCourse, id)
	var i SubCourse
	err := row.Scan(
		&i.ID,
		&i.CourseID,
		&i.Title,
		&i.Description,
		&i.Thumbnail,
		&i.DisplayOrder,
		&i.Level,
		&i.IsActive,
	)
	return i, err
}

const GetSubCourseByID = `-- name: GetSubCourseByID :one
SELECT id, course_id, title, description, thumbnail, display_order, level, is_active
FROM sub_courses
WHERE id = $1
`

func (q *Queries) GetSubCourseByID(ctx context.Context, id int64) (SubCourse, error) {
	row := q.db.QueryRow(ctx, GetSubCourseByID, id)
	var i SubCourse
	err := row.Scan(
		&i.ID,
		&i.CourseID,
		&i.Title,
		&i.Description,
		&i.Thumbnail,
		&i.DisplayOrder,
		&i.Level,
		&i.IsActive,
	)
	return i, err
}

const GetSubCoursesByCourse = `-- name: GetSubCoursesByCourse :many
SELECT
    COUNT(*) OVER () AS total_count,
    id,
    course_id,
    title,
    description,
    thumbnail,
    display_order,
    level,
    is_active
FROM sub_courses
WHERE course_id = $1
ORDER BY display_order ASC, id ASC
`

type GetSubCoursesByCourseRow struct {
	TotalCount   int64       `json:"total_count"`
	ID           int64       `json:"id"`
	CourseID     int64       `json:"course_id"`
	Title        string      `json:"title"`
	Description  pgtype.Text `json:"description"`
	Thumbnail    pgtype.Text `json:"thumbnail"`
	DisplayOrder int32       `json:"display_order"`
	Level        string      `json:"level"`
	IsActive     bool        `json:"is_active"`
}

func (q *Queries) GetSubCoursesByCourse(ctx context.Context, courseID int64) ([]GetSubCoursesByCourseRow, error) {
	rows, err := q.db.Query(ctx, GetSubCoursesByCourse, courseID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetSubCoursesByCourseRow
	for rows.Next() {
		var i GetSubCoursesByCourseRow
		if err := rows.Scan(
			&i.TotalCount,
			&i.ID,
			&i.CourseID,
			&i.Title,
			&i.Description,
			&i.Thumbnail,
			&i.DisplayOrder,
			&i.Level,
			&i.IsActive,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const ListActiveSubCourses = `-- name: ListActiveSubCourses :many
SELECT
    id,
    course_id,
    title,
    description,
    thumbnail,
    display_order,
    level,
    is_active
FROM sub_courses
WHERE is_active = TRUE
ORDER BY display_order ASC
`

func (q *Queries) ListActiveSubCourses(ctx context.Context) ([]SubCourse, error) {
	rows, err := q.db.Query(ctx, ListActiveSubCourses)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []SubCourse
	for rows.Next() {
		var i SubCourse
		if err := rows.Scan(
			&i.ID,
			&i.CourseID,
			&i.Title,
			&i.Description,
			&i.Thumbnail,
			&i.DisplayOrder,
			&i.Level,
			&i.IsActive,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const ListSubCoursesByCourse = `-- name: ListSubCoursesByCourse :many
SELECT
    id,
    course_id,
    title,
    description,
    thumbnail,
    display_order,
    level,
    is_active
FROM sub_courses
WHERE course_id = $1
  AND is_active = TRUE
ORDER BY display_order ASC, id ASC
`

func (q *Queries) ListSubCoursesByCourse(ctx context.Context, courseID int64) ([]SubCourse, error) {
	rows, err := q.db.Query(ctx, ListSubCoursesByCourse, courseID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []SubCourse
	for rows.Next() {
		var i SubCourse
		if err := rows.Scan(
			&i.ID,
			&i.CourseID,
			&i.Title,
			&i.Description,
			&i.Thumbnail,
			&i.DisplayOrder,
			&i.Level,
			&i.IsActive,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const UpdateSubCourse = `-- name: UpdateSubCourse :exec
UPDATE sub_courses
SET
    title = COALESCE($1, title),
    description = COALESCE($2, description),
    thumbnail = COALESCE($3, thumbnail),
    display_order = COALESCE($4, display_order),
    level = COALESCE($5, level),
    is_active = COALESCE($6, is_active)
WHERE id = $7
`

type UpdateSubCourseParams struct {
	Title        string      `json:"title"`
	Description  pgtype.Text `json:"description"`
	Thumbnail    pgtype.Text `json:"thumbnail"`
	DisplayOrder int32       `json:"display_order"`
	Level        string      `json:"level"`
	IsActive     bool        `json:"is_active"`
	ID           int64       `json:"id"`
}

func (q *Queries) UpdateSubCourse(ctx context.Context, arg UpdateSubCourseParams) error {
	_, err := q.db.Exec(ctx, UpdateSubCourse,
		arg.Title,
		arg.Description,
		arg.Thumbnail,
		arg.DisplayOrder,
		arg.Level,
		arg.IsActive,
		arg.ID,
	)
	return err
}
