-- 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;

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

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

-- name: CountUsers :one
SELECT COUNT(*)::int FROM users;

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

-- name: GetSessionByTokenHash :one
SELECT id, user_id, token_hash, expires_at, last_seen_at, created_at
FROM user_sessions
WHERE token_hash = $1
  AND expires_at > now();

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

-- name: DeleteSessionByTokenHash :exec
DELETE FROM user_sessions WHERE token_hash = $1;

-- name: DeleteExpiredSessions :exec
DELETE FROM user_sessions WHERE expires_at <= now();

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