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

package repository

import (
	"context"
)

const countApiKeys = `-- name: CountApiKeys :one
SELECT COUNT(*) FROM api_keys WHERE user_id = $1
`

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

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

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

const deleteApiKey = `-- name: DeleteApiKey :exec
DELETE FROM api_keys WHERE id = $1
`

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

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

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

const getAllApiKeysByUserID = `-- name: GetAllApiKeysByUserID :many
SELECT ak.id, ak.key, ak.user_id, ak.created_at, ak.label
FROM api_keys ak 
JOIN users u ON ak.user_id = u.id 
WHERE u.id = $1
`

func (q *Queries) GetAllApiKeysByUserID(ctx context.Context, id int32) ([]ApiKey, error) {
	rows, err := q.db.Query(ctx, getAllApiKeysByUserID, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ApiKey
	for rows.Next() {
		var i ApiKey
		if err := rows.Scan(
			&i.ID,
			&i.Key,
			&i.UserID,
			&i.CreatedAt,
			&i.Label,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserByApiKey = `-- name: GetUserByApiKey :one
SELECT u.id, u.username, u.role, u.password 
FROM users u
JOIN api_keys ak ON u.id = ak.user_id 
WHERE ak.key = $1
`

func (q *Queries) GetUserByApiKey(ctx context.Context, key string) (User, error) {
	row := q.db.QueryRow(ctx, getUserByApiKey, key)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Role,
		&i.Password,
	)
	return i, err
}

const getUserByUsername = `-- name: GetUserByUsername :one
SELECT id, username, role, password FROM users WHERE username = $1
`

func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error) {
	row := q.db.QueryRow(ctx, getUserByUsername, username)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Role,
		&i.Password,
	)
	return i, err
}

const insertApiKey = `-- name: InsertApiKey :one
INSERT INTO api_keys (user_id, key, label)
VALUES ($1, $2, $3)
RETURNING id, key, user_id, created_at, label
`

type InsertApiKeyParams struct {
	UserID int32
	Key    string
	Label  string
}

func (q *Queries) InsertApiKey(ctx context.Context, arg InsertApiKeyParams) (ApiKey, error) {
	row := q.db.QueryRow(ctx, insertApiKey, arg.UserID, arg.Key, arg.Label)
	var i ApiKey
	err := row.Scan(
		&i.ID,
		&i.Key,
		&i.UserID,
		&i.CreatedAt,
		&i.Label,
	)
	return i, err
}

const insertUser = `-- name: InsertUser :one
INSERT INTO users (username, password, role)
VALUES ($1, $2, $3)
RETURNING id, username, role, password
`

type InsertUserParams struct {
	Username string
	Password []byte
	Role     Role
}

func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (User, error) {
	row := q.db.QueryRow(ctx, insertUser, arg.Username, arg.Password, arg.Role)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Role,
		&i.Password,
	)
	return i, err
}

const updateApiKeyLabel = `-- name: UpdateApiKeyLabel :exec
UPDATE api_keys SET label = $3 WHERE id = $1 AND user_id = $2
`

type UpdateApiKeyLabelParams struct {
	ID     int32
	UserID int32
	Label  string
}

func (q *Queries) UpdateApiKeyLabel(ctx context.Context, arg UpdateApiKeyLabelParams) error {
	_, err := q.db.Exec(ctx, updateApiKeyLabel, arg.ID, arg.UserID, arg.Label)
	return err
}

const updateUserPassword = `-- name: UpdateUserPassword :exec
UPDATE users SET password = $2 WHERE id = $1
`

type UpdateUserPasswordParams struct {
	ID       int32
	Password []byte
}

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

const updateUserUsername = `-- name: UpdateUserUsername :exec
UPDATE users SET username = $2 WHERE id = $1
`

type UpdateUserUsernameParams struct {
	ID       int32
	Username string
}

func (q *Queries) UpdateUserUsername(ctx context.Context, arg UpdateUserUsernameParams) error {
	_, err := q.db.Exec(ctx, updateUserUsername, arg.ID, arg.Username)
	return err
}
