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

package sqlc

import (
	"context"

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

const countAccounts = `-- name: CountAccounts :one
SELECT COUNT(*)::bigint AS count
FROM users
WHERE username IS NOT NULL
  AND password_hash IS NOT NULL
`

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

const createAccount = `-- name: CreateAccount :one
UPDATE users
SET username = $1,
    email = $2,
    password_hash = $3,
    role = $4::user_role,
    display_name = $5,
    avatar_url = $6,
    is_active = $7,
    data_root = $8,
    updated_at = now()
WHERE id = $9
RETURNING id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at
`

type CreateAccountParams struct {
	Username     pgtype.Text `json:"username"`
	Email        pgtype.Text `json:"email"`
	PasswordHash pgtype.Text `json:"password_hash"`
	Role         string      `json:"role"`
	DisplayName  pgtype.Text `json:"display_name"`
	AvatarUrl    pgtype.Text `json:"avatar_url"`
	IsActive     bool        `json:"is_active"`
	DataRoot     pgtype.Text `json:"data_root"`
	UserID       pgtype.UUID `json:"user_id"`
}

func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (User, error) {
	row := q.db.QueryRow(ctx, createAccount,
		arg.Username,
		arg.Email,
		arg.PasswordHash,
		arg.Role,
		arg.DisplayName,
		arg.AvatarUrl,
		arg.IsActive,
		arg.DataRoot,
		arg.UserID,
	)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createUser = `-- name: CreateUser :one
INSERT INTO users (is_active, metadata)
VALUES ($1, $2)
RETURNING id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at
`

type CreateUserParams struct {
	IsActive bool   `json:"is_active"`
	Metadata []byte `json:"metadata"`
}

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

const getAccountByIdentity = `-- name: GetAccountByIdentity :one
SELECT id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at FROM users WHERE username = $1 OR email = $1
`

func (q *Queries) GetAccountByIdentity(ctx context.Context, identity pgtype.Text) (User, error) {
	row := q.db.QueryRow(ctx, getAccountByIdentity, identity)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getAccountByUserID = `-- name: GetAccountByUserID :one
SELECT id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at FROM users WHERE id = $1
`

func (q *Queries) GetAccountByUserID(ctx context.Context, userID pgtype.UUID) (User, error) {
	row := q.db.QueryRow(ctx, getAccountByUserID, userID)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getUserByID = `-- name: GetUserByID :one
SELECT id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at
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.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listAccounts = `-- name: ListAccounts :many
SELECT id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at FROM users
WHERE username IS NOT NULL
ORDER BY created_at DESC
`

func (q *Queries) ListAccounts(ctx context.Context) ([]User, error) {
	rows, err := q.db.Query(ctx, listAccounts)
	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.Username,
			&i.Email,
			&i.PasswordHash,
			&i.Role,
			&i.DisplayName,
			&i.AvatarUrl,
			&i.DataRoot,
			&i.LastLoginAt,
			&i.IsActive,
			&i.Metadata,
			&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 updateAccountAdmin = `-- name: UpdateAccountAdmin :one
UPDATE users
SET role = $1::user_role,
    display_name = $2,
    avatar_url = $3,
    is_active = $4,
    updated_at = now()
WHERE id = $5
RETURNING id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at
`

type UpdateAccountAdminParams struct {
	Role        string      `json:"role"`
	DisplayName pgtype.Text `json:"display_name"`
	AvatarUrl   pgtype.Text `json:"avatar_url"`
	IsActive    bool        `json:"is_active"`
	UserID      pgtype.UUID `json:"user_id"`
}

func (q *Queries) UpdateAccountAdmin(ctx context.Context, arg UpdateAccountAdminParams) (User, error) {
	row := q.db.QueryRow(ctx, updateAccountAdmin,
		arg.Role,
		arg.DisplayName,
		arg.AvatarUrl,
		arg.IsActive,
		arg.UserID,
	)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const updateAccountLastLogin = `-- name: UpdateAccountLastLogin :one
UPDATE users
SET last_login_at = now(),
    updated_at = now()
WHERE id = $1
RETURNING id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at
`

func (q *Queries) UpdateAccountLastLogin(ctx context.Context, id pgtype.UUID) (User, error) {
	row := q.db.QueryRow(ctx, updateAccountLastLogin, id)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const updateAccountPassword = `-- name: UpdateAccountPassword :one
UPDATE users
SET password_hash = $2,
    updated_at = now()
WHERE id = $1
RETURNING id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at
`

type UpdateAccountPasswordParams struct {
	ID           pgtype.UUID `json:"id"`
	PasswordHash pgtype.Text `json:"password_hash"`
}

func (q *Queries) UpdateAccountPassword(ctx context.Context, arg UpdateAccountPasswordParams) (User, error) {
	row := q.db.QueryRow(ctx, updateAccountPassword, arg.ID, arg.PasswordHash)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const updateAccountProfile = `-- name: UpdateAccountProfile :one
UPDATE users
SET display_name = $2,
    avatar_url = $3,
    is_active = $4,
    updated_at = now()
WHERE id = $1
RETURNING id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at
`

type UpdateAccountProfileParams struct {
	ID          pgtype.UUID `json:"id"`
	DisplayName pgtype.Text `json:"display_name"`
	AvatarUrl   pgtype.Text `json:"avatar_url"`
	IsActive    bool        `json:"is_active"`
}

func (q *Queries) UpdateAccountProfile(ctx context.Context, arg UpdateAccountProfileParams) (User, error) {
	row := q.db.QueryRow(ctx, updateAccountProfile,
		arg.ID,
		arg.DisplayName,
		arg.AvatarUrl,
		arg.IsActive,
	)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const upsertAccountByUsername = `-- name: UpsertAccountByUsername :one
INSERT INTO users (id, username, email, password_hash, role, display_name, avatar_url, is_active, data_root, metadata)
VALUES (
  $1,
  $2,
  $3,
  $4,
  $5::user_role,
  $6,
  $7,
  $8,
  $9,
  '{}'::jsonb
)
ON CONFLICT (username) DO UPDATE SET
  email = EXCLUDED.email,
  password_hash = EXCLUDED.password_hash,
  role = EXCLUDED.role,
  display_name = EXCLUDED.display_name,
  avatar_url = EXCLUDED.avatar_url,
  is_active = EXCLUDED.is_active,
  data_root = EXCLUDED.data_root,
  updated_at = now()
RETURNING id, username, email, password_hash, role, display_name, avatar_url, data_root, last_login_at, is_active, metadata, created_at, updated_at
`

type UpsertAccountByUsernameParams struct {
	UserID       pgtype.UUID `json:"user_id"`
	Username     pgtype.Text `json:"username"`
	Email        pgtype.Text `json:"email"`
	PasswordHash pgtype.Text `json:"password_hash"`
	Role         string      `json:"role"`
	DisplayName  pgtype.Text `json:"display_name"`
	AvatarUrl    pgtype.Text `json:"avatar_url"`
	IsActive     bool        `json:"is_active"`
	DataRoot     pgtype.Text `json:"data_root"`
}

func (q *Queries) UpsertAccountByUsername(ctx context.Context, arg UpsertAccountByUsernameParams) (User, error) {
	row := q.db.QueryRow(ctx, upsertAccountByUsername,
		arg.UserID,
		arg.Username,
		arg.Email,
		arg.PasswordHash,
		arg.Role,
		arg.DisplayName,
		arg.AvatarUrl,
		arg.IsActive,
		arg.DataRoot,
	)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.PasswordHash,
		&i.Role,
		&i.DisplayName,
		&i.AvatarUrl,
		&i.DataRoot,
		&i.LastLoginAt,
		&i.IsActive,
		&i.Metadata,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
