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

package dbgen

import (
	"context"
)

const getRoleByID = `-- name: GetRoleByID :one
SELECT id, name, color, permissions, position, is_default
FROM roles WHERE id = ?
`

func (q *Queries) GetRoleByID(ctx context.Context, id int64) (Role, error) {
	row := q.db.QueryRowContext(ctx, getRoleByID, id)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Color,
		&i.Permissions,
		&i.Position,
		&i.IsDefault,
	)
	return i, err
}

const getRoleForUser = `-- name: GetRoleForUser :one
SELECT r.id, r.name, r.color, r.permissions, r.position, r.is_default
FROM users u
JOIN roles r ON u.role_id = r.id
WHERE u.id = ?
`

func (q *Queries) GetRoleForUser(ctx context.Context, id int64) (Role, error) {
	row := q.db.QueryRowContext(ctx, getRoleForUser, id)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Color,
		&i.Permissions,
		&i.Position,
		&i.IsDefault,
	)
	return i, err
}

const getUserWithRole = `-- name: GetUserWithRole :one
SELECT u.id, u.username, u.password, u.avatar, u.role_id,
       u.totp_secret, u.status, u.created_at, u.last_seen,
       u.banned, u.ban_reason, u.ban_expires,
       r.id, r.name, r.color, r.permissions, r.position, r.is_default
FROM users u
JOIN roles r ON r.id = u.role_id
WHERE u.id = ?
`

type GetUserWithRoleRow struct {
	ID          int64   `json:"id"`
	Username    string  `json:"username"`
	Password    string  `json:"password"`
	Avatar      *string `json:"avatar"`
	RoleID      int64   `json:"roleId"`
	TotpSecret  *string `json:"totpSecret"`
	Status      string  `json:"status"`
	CreatedAt   string  `json:"createdAt"`
	LastSeen    *string `json:"lastSeen"`
	Banned      int64   `json:"banned"`
	BanReason   *string `json:"banReason"`
	BanExpires  *string `json:"banExpires"`
	ID_2        int64   `json:"id2"`
	Name        string  `json:"name"`
	Color       *string `json:"color"`
	Permissions int64   `json:"permissions"`
	Position    int64   `json:"position"`
	IsDefault   int64   `json:"isDefault"`
}

func (q *Queries) GetUserWithRole(ctx context.Context, id int64) (GetUserWithRoleRow, error) {
	row := q.db.QueryRowContext(ctx, getUserWithRole, id)
	var i GetUserWithRoleRow
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Password,
		&i.Avatar,
		&i.RoleID,
		&i.TotpSecret,
		&i.Status,
		&i.CreatedAt,
		&i.LastSeen,
		&i.Banned,
		&i.BanReason,
		&i.BanExpires,
		&i.ID_2,
		&i.Name,
		&i.Color,
		&i.Permissions,
		&i.Position,
		&i.IsDefault,
	)
	return i, err
}

const listRoles = `-- name: ListRoles :many
SELECT id, name, color, permissions, position, is_default
FROM roles ORDER BY position DESC
`

func (q *Queries) ListRoles(ctx context.Context) ([]Role, error) {
	rows, err := q.db.QueryContext(ctx, listRoles)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Role{}
	for rows.Next() {
		var i Role
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Color,
			&i.Permissions,
			&i.Position,
			&i.IsDefault,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
