-- name: CreateUserSession :one
INSERT INTO user_sessions (
    user_id, session_type, issued_at, expires_at, last_active,
    ip_address, user_agent,
    device_info, is_active
) VALUES (
    $1, $2, NOW(), $3, $4,
    $5, $6,
    $7, TRUE
)
RETURNING *;

-- name: ListActiveUserSessions :many
SELECT * FROM user_sessions
WHERE user_id = $1
  AND is_active = TRUE
ORDER BY issued_at DESC;

-- name: GetUserSessionByToken :one
SELECT * FROM user_sessions
WHERE refresh_token = $1
  AND is_active = TRUE;

-- name: RevokeUserSession :exec
UPDATE user_sessions
SET is_active = FALSE,
    revoked_at = NOW()
WHERE id = $1
  AND is_active = TRUE;

-- name: UpdateSessionLastActive :exec
UPDATE user_sessions
SET last_active = NOW()
WHERE id = $1
  AND is_active = TRUE;

-- name: ListAllSessions :many
SELECT * FROM user_sessions
ORDER BY issued_at DESC
LIMIT $1 OFFSET $2;
