// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: users.sql

package usersqlc

import (
	"context"

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

const countAdmins = `-- name: CountAdmins :one
SELECT COUNT(*)::int FROM users WHERE is_admin = true AND enabled = true
`

func (q *Queries) CountAdmins(ctx context.Context) (int32, error) {
	row := q.db.QueryRow(ctx, countAdmins)
	var column_1 int32
	err := row.Scan(&column_1)
	return column_1, err
}

const countUsers = `-- name: CountUsers :one
SELECT COUNT(*)::int FROM users
`

func (q *Queries) CountUsers(ctx context.Context) (int32, error) {
	row := q.db.QueryRow(ctx, countUsers)
	var column_1 int32
	err := row.Scan(&column_1)
	return column_1, err
}

const createSession = `-- name: CreateSession :exec
INSERT INTO user_sessions (id, user_id, token_hash, expires_at)
VALUES ($1, $2, $3, $4)
`

type CreateSessionParams struct {
	ID        pgtype.UUID        `json:"id"`
	UserID    pgtype.UUID        `json:"user_id"`
	TokenHash []byte             `json:"token_hash"`
	ExpiresAt pgtype.Timestamptz `json:"expires_at"`
}

func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) error {
	_, err := q.db.Exec(ctx, createSession,
		arg.ID,
		arg.UserID,
		arg.TokenHash,
		arg.ExpiresAt,
	)
	return err
}

const createUser = `-- name: CreateUser :one
INSERT INTO users (id, email, password_hash, display_name, is_admin)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, email, password_hash, display_name, is_admin, created_at, updated_at, enabled
`

type CreateUserParams struct {
	ID           pgtype.UUID `json:"id"`
	Email        string      `json:"email"`
	PasswordHash string      `json:"password_hash"`
	DisplayName  string      `json:"display_name"`
	IsAdmin      bool        `json:"is_admin"`
}

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
	row := q.db.QueryRow(ctx, createUser,
		arg.ID,
		arg.Email,
		arg.PasswordHash,
		arg.DisplayName,
		arg.IsAdmin,
	)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Email,
		&i.PasswordHash,
		&i.DisplayName,
		&i.IsAdmin,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
	)
	return i, err
}

const deleteExpiredSessions = `-- name: DeleteExpiredSessions :exec
DELETE FROM user_sessions WHERE expires_at <= now()
`

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

const deleteSessionByTokenHash = `-- name: DeleteSessionByTokenHash :exec
DELETE FROM user_sessions WHERE token_hash = $1
`

func (q *Queries) DeleteSessionByTokenHash(ctx context.Context, tokenHash []byte) error {
	_, err := q.db.Exec(ctx, deleteSessionByTokenHash, tokenHash)
	return err
}

const deleteSessionsByUserID = `-- name: DeleteSessionsByUserID :exec
DELETE FROM user_sessions WHERE user_id = $1
`

func (q *Queries) DeleteSessionsByUserID(ctx context.Context, userID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteSessionsByUserID, userID)
	return err
}

const deleteUser = `-- name: DeleteUser :exec
DELETE FROM users WHERE id = $1
`

func (q *Queries) DeleteUser(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteUser, id)
	return err
}

const getSessionByTokenHash = `-- name: GetSessionByTokenHash :one
SELECT s.id, s.user_id, s.token_hash, s.expires_at, s.last_seen_at, s.created_at
FROM user_sessions s
JOIN users u ON u.id = s.user_id
WHERE s.token_hash = $1
  AND s.expires_at > now()
  AND u.enabled = true
`

func (q *Queries) GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (UserSession, error) {
	row := q.db.QueryRow(ctx, getSessionByTokenHash, tokenHash)
	var i UserSession
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.TokenHash,
		&i.ExpiresAt,
		&i.LastSeenAt,
		&i.CreatedAt,
	)
	return i, err
}

const getUserByEmail = `-- name: GetUserByEmail :one
SELECT id, email, password_hash, display_name, is_admin, created_at, updated_at, enabled
FROM users
WHERE email = $1
`

func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error) {
	row := q.db.QueryRow(ctx, getUserByEmail, email)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Email,
		&i.PasswordHash,
		&i.DisplayName,
		&i.IsAdmin,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
	)
	return i, err
}

const getUserByID = `-- name: GetUserByID :one
SELECT id, email, password_hash, display_name, is_admin, created_at, updated_at, enabled
FROM users
WHERE id = $1
`

func (q *Queries) GetUserByID(ctx context.Context, id pgtype.UUID) (User, error) {
	row := q.db.QueryRow(ctx, getUserByID, id)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Email,
		&i.PasswordHash,
		&i.DisplayName,
		&i.IsAdmin,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
	)
	return i, err
}

const listAdmins = `-- name: ListAdmins :many
SELECT id, email, password_hash, display_name, is_admin, created_at, updated_at, enabled
  FROM users
 WHERE is_admin = true
 ORDER BY id
`

func (q *Queries) ListAdmins(ctx context.Context) ([]User, error) {
	rows, err := q.db.Query(ctx, listAdmins)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []User
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Email,
			&i.PasswordHash,
			&i.DisplayName,
			&i.IsAdmin,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Enabled,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listUsers = `-- name: ListUsers :many
SELECT id, email, password_hash, display_name, is_admin, created_at, updated_at, enabled
FROM users
ORDER BY created_at ASC
`

func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
	rows, err := q.db.Query(ctx, listUsers)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []User
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Email,
			&i.PasswordHash,
			&i.DisplayName,
			&i.IsAdmin,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Enabled,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const setUserAdmin = `-- name: SetUserAdmin :one
UPDATE users
SET is_admin = $2, updated_at = now()
WHERE id = $1
RETURNING id, email, password_hash, display_name, is_admin, created_at, updated_at, enabled
`

type SetUserAdminParams struct {
	ID      pgtype.UUID `json:"id"`
	IsAdmin bool        `json:"is_admin"`
}

func (q *Queries) SetUserAdmin(ctx context.Context, arg SetUserAdminParams) (User, error) {
	row := q.db.QueryRow(ctx, setUserAdmin, arg.ID, arg.IsAdmin)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Email,
		&i.PasswordHash,
		&i.DisplayName,
		&i.IsAdmin,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
	)
	return i, err
}

const setUserEnabled = `-- name: SetUserEnabled :one
UPDATE users
SET enabled = $2, updated_at = now()
WHERE id = $1
RETURNING id, email, password_hash, display_name, is_admin, created_at, updated_at, enabled
`

type SetUserEnabledParams struct {
	ID      pgtype.UUID `json:"id"`
	Enabled bool        `json:"enabled"`
}

func (q *Queries) SetUserEnabled(ctx context.Context, arg SetUserEnabledParams) (User, error) {
	row := q.db.QueryRow(ctx, setUserEnabled, arg.ID, arg.Enabled)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Email,
		&i.PasswordHash,
		&i.DisplayName,
		&i.IsAdmin,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
	)
	return i, err
}

const touchSession = `-- name: TouchSession :exec
UPDATE user_sessions
SET last_seen_at = now()
WHERE token_hash = $1
`

func (q *Queries) TouchSession(ctx context.Context, tokenHash []byte) error {
	_, err := q.db.Exec(ctx, touchSession, tokenHash)
	return err
}

const updateUserPassword = `-- name: UpdateUserPassword :exec
UPDATE users
SET password_hash = $2, updated_at = now()
WHERE id = $1
`

type UpdateUserPasswordParams struct {
	ID           pgtype.UUID `json:"id"`
	PasswordHash string      `json:"password_hash"`
}

func (q *Queries) UpdateUserPassword(ctx context.Context, arg UpdateUserPasswordParams) error {
	_, err := q.db.Exec(ctx, updateUserPassword, arg.ID, arg.PasswordHash)
	return err
}

const updateUserProfile = `-- name: UpdateUserProfile :one
UPDATE users
SET display_name = $2, updated_at = now()
WHERE id = $1
RETURNING id, email, password_hash, display_name, is_admin, created_at, updated_at, enabled
`

type UpdateUserProfileParams struct {
	ID          pgtype.UUID `json:"id"`
	DisplayName string      `json:"display_name"`
}

func (q *Queries) UpdateUserProfile(ctx context.Context, arg UpdateUserProfileParams) (User, error) {
	row := q.db.QueryRow(ctx, updateUserProfile, arg.ID, arg.DisplayName)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Email,
		&i.PasswordHash,
		&i.DisplayName,
		&i.IsAdmin,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
	)
	return i, err
}
