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

package dbgen

import (
	"context"
)

const countActiveInvites = `-- name: CountActiveInvites :one
SELECT COUNT(*) FROM invites WHERE revoked = 0
`

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

const countActiveMessages = `-- name: CountActiveMessages :one
SELECT COUNT(*) FROM messages WHERE deleted = 0
`

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

const countChannels = `-- name: CountChannels :one
SELECT COUNT(*) FROM channels
`

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

const forceLogoutUser = `-- name: ForceLogoutUser :exec
DELETE FROM sessions WHERE user_id = ?
`

func (q *Queries) ForceLogoutUser(ctx context.Context, userID int64) error {
	_, err := q.db.ExecContext(ctx, forceLogoutUser, userID)
	return err
}

const getAllSettings = `-- name: GetAllSettings :many
SELECT key, value FROM settings
`

func (q *Queries) GetAllSettings(ctx context.Context) ([]Setting, error) {
	rows, err := q.db.QueryContext(ctx, getAllSettings)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Setting{}
	for rows.Next() {
		var i Setting
		if err := rows.Scan(&i.Key, &i.Value); 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
}

const getAuditLog = `-- name: GetAuditLog :many
SELECT a.id, a.actor_id, COALESCE(u.username, '') AS actor_name, a.action,
       a.target_type, a.target_id, a.detail, a.created_at
FROM audit_log a
LEFT JOIN users u ON u.id = a.actor_id
ORDER BY a.id DESC
LIMIT ? OFFSET ?
`

type GetAuditLogParams struct {
	Limit  int64 `json:"limit"`
	Offset int64 `json:"offset"`
}

type GetAuditLogRow struct {
	ID         int64  `json:"id"`
	ActorID    int64  `json:"actorId"`
	ActorName  string `json:"actorName"`
	Action     string `json:"action"`
	TargetType string `json:"targetType"`
	TargetID   int64  `json:"targetId"`
	Detail     string `json:"detail"`
	CreatedAt  string `json:"createdAt"`
}

func (q *Queries) GetAuditLog(ctx context.Context, arg GetAuditLogParams) ([]GetAuditLogRow, error) {
	rows, err := q.db.QueryContext(ctx, getAuditLog, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetAuditLogRow{}
	for rows.Next() {
		var i GetAuditLogRow
		if err := rows.Scan(
			&i.ID,
			&i.ActorID,
			&i.ActorName,
			&i.Action,
			&i.TargetType,
			&i.TargetID,
			&i.Detail,
			&i.CreatedAt,
		); 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
}

const getSetting = `-- name: GetSetting :one
SELECT value FROM settings WHERE key = ?
`

func (q *Queries) GetSetting(ctx context.Context, key string) (string, error) {
	row := q.db.QueryRowContext(ctx, getSetting, key)
	var value string
	err := row.Scan(&value)
	return value, err
}

const getUserSessions = `-- name: GetUserSessions :many
SELECT id, user_id, token, device, ip_address, created_at, last_used, expires_at
FROM sessions WHERE user_id = ?
ORDER BY created_at DESC
`

func (q *Queries) GetUserSessions(ctx context.Context, userID int64) ([]Session, error) {
	rows, err := q.db.QueryContext(ctx, getUserSessions, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Session{}
	for rows.Next() {
		var i Session
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Token,
			&i.Device,
			&i.IpAddress,
			&i.CreatedAt,
			&i.LastUsed,
			&i.ExpiresAt,
		); 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
}

const listAllUsers = `-- name: ListAllUsers :many
SELECT u.id, u.username, u.avatar, u.role_id,
       u.status, u.created_at, u.last_seen, u.banned, u.ban_reason, u.ban_expires,
       COALESCE(r.name, '') AS role_name
FROM users u
LEFT JOIN roles r ON r.id = u.role_id
ORDER BY u.id ASC
LIMIT ? OFFSET ?
`

type ListAllUsersParams struct {
	Limit  int64 `json:"limit"`
	Offset int64 `json:"offset"`
}

type ListAllUsersRow struct {
	ID         int64   `json:"id"`
	Username   string  `json:"username"`
	Avatar     *string `json:"avatar"`
	RoleID     int64   `json:"roleId"`
	Status     string  `json:"status"`
	CreatedAt  string  `json:"createdAt"`
	LastSeen   *string `json:"lastSeen"`
	Banned     int64   `json:"banned"`
	BanReason  *string `json:"banReason"`
	BanExpires *string `json:"banExpires"`
	RoleName   string  `json:"roleName"`
}

func (q *Queries) ListAllUsers(ctx context.Context, arg ListAllUsersParams) ([]ListAllUsersRow, error) {
	rows, err := q.db.QueryContext(ctx, listAllUsers, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListAllUsersRow{}
	for rows.Next() {
		var i ListAllUsersRow
		if err := rows.Scan(
			&i.ID,
			&i.Username,
			&i.Avatar,
			&i.RoleID,
			&i.Status,
			&i.CreatedAt,
			&i.LastSeen,
			&i.Banned,
			&i.BanReason,
			&i.BanExpires,
			&i.RoleName,
		); 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
}

const logAudit = `-- name: LogAudit :exec
INSERT INTO audit_log (actor_id, action, target_type, target_id, detail)
VALUES (?, ?, ?, ?, ?)
`

type LogAuditParams struct {
	ActorID    int64  `json:"actorId"`
	Action     string `json:"action"`
	TargetType string `json:"targetType"`
	TargetID   int64  `json:"targetId"`
	Detail     string `json:"detail"`
}

func (q *Queries) LogAudit(ctx context.Context, arg LogAuditParams) error {
	_, err := q.db.ExecContext(ctx, logAudit,
		arg.ActorID,
		arg.Action,
		arg.TargetType,
		arg.TargetID,
		arg.Detail,
	)
	return err
}

const setSetting = `-- name: SetSetting :exec
INSERT INTO settings (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value
`

type SetSettingParams struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

func (q *Queries) SetSetting(ctx context.Context, arg SetSettingParams) error {
	_, err := q.db.ExecContext(ctx, setSetting, arg.Key, arg.Value)
	return err
}

const updateUserRole = `-- name: UpdateUserRole :exec
UPDATE users SET role_id = ? WHERE id = ?
`

type UpdateUserRoleParams struct {
	RoleID int64 `json:"roleId"`
	ID     int64 `json:"id"`
}

func (q *Queries) UpdateUserRole(ctx context.Context, arg UpdateUserRoleParams) error {
	_, err := q.db.ExecContext(ctx, updateUserRole, arg.RoleID, arg.ID)
	return err
}

const userCount = `-- name: UserCount :one
SELECT COUNT(*) FROM users
`

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