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

package sqlc

import (
	"context"

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

const addStudentToClassroom = `-- name: AddStudentToClassroom :exec
INSERT INTO classroom_students (
    classroom_id,
    student_id
) VALUES (
    $1,
    $2
)
ON CONFLICT (classroom_id, student_id) DO NOTHING
`

type AddStudentToClassroomParams struct {
	ClassroomID int64 `json:"classroom_id"`
	StudentID   int64 `json:"student_id"`
}

func (q *Queries) AddStudentToClassroom(ctx context.Context, arg AddStudentToClassroomParams) error {
	_, err := q.db.Exec(ctx, addStudentToClassroom, arg.ClassroomID, arg.StudentID)
	return err
}

const createClassroom = `-- name: CreateClassroom :one
INSERT INTO classrooms (
    teacher_id,
    name,
    code,
    description
) VALUES (
    $1,
    $2,
    $3,
    $4
)
RETURNING id, teacher_id, name, code, description, created_at, updated_at
`

type CreateClassroomParams struct {
	TeacherID   int64       `json:"teacher_id"`
	Name        string      `json:"name"`
	Code        pgtype.Text `json:"code"`
	Description pgtype.Text `json:"description"`
}

func (q *Queries) CreateClassroom(ctx context.Context, arg CreateClassroomParams) (Classroom, error) {
	row := q.db.QueryRow(ctx, createClassroom,
		arg.TeacherID,
		arg.Name,
		arg.Code,
		arg.Description,
	)
	var i Classroom
	err := row.Scan(
		&i.ID,
		&i.TeacherID,
		&i.Name,
		&i.Code,
		&i.Description,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listClassroomsByTeacher = `-- name: ListClassroomsByTeacher :many
SELECT id, teacher_id, name, code, description, created_at, updated_at
FROM classrooms
WHERE teacher_id = $1
ORDER BY created_at DESC
`

func (q *Queries) ListClassroomsByTeacher(ctx context.Context, teacherID int64) ([]Classroom, error) {
	rows, err := q.db.Query(ctx, listClassroomsByTeacher, teacherID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Classroom{}
	for rows.Next() {
		var i Classroom
		if err := rows.Scan(
			&i.ID,
			&i.TeacherID,
			&i.Name,
			&i.Code,
			&i.Description,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listStudentsForClassroom = `-- name: ListStudentsForClassroom :many
SELECT u.id, u.email, u.password_hash, u.role, u.full_name, u.is_active, u.created_at, u.updated_at
FROM classroom_students cs
JOIN users u ON u.id = cs.student_id
WHERE cs.classroom_id = $1
ORDER BY u.full_name ASC
`

func (q *Queries) ListStudentsForClassroom(ctx context.Context, classroomID int64) ([]User, error) {
	rows, err := q.db.Query(ctx, listStudentsForClassroom, classroomID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Email,
			&i.PasswordHash,
			&i.Role,
			&i.FullName,
			&i.IsActive,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
