-- name: RegisterAccount :one
SELECT auth.bh_register_account(
	sqlc.arg(email),
	sqlc.arg(passwordHash),
	sqlc.arg(role)
);

-- name: GetAccountByEmailOrId :one
SELECT
	id,
	createdTs,
	verifiedTs,
	email,
	role,
	enabled
FROM auth.account
WHERE
	email = sqlc.arg(email) OR id = sqlc.arg(accountId);

-- name: GetSessionAccount :one
SELECT
	acc.id,
	acc.createdTs,
	acc.verifiedTs,
	acc.email,
	acc.role,
	acc.enabled
FROM auth.account acc
JOIN auth.sessions sess ON sess.accountId = acc.id
WHERE sess.id = sqlc.arg(sessionId) AND acc.enabled = TRUE;


-- name: VerifyAccount :one
SELECT auth.bh_verify_account(
	sqlc.arg(token)
);


-- name: CreateSession :one
INSERT INTO auth.sessions (
	accountId,
	createdTs
) SELECT acc.id, NOW() FROM auth.account acc WHERE
(acc.email = sqlc.arg(email) OR acc.id = sqlc.arg(accountId))
AND acc.passwordHash = sqlc.arg(passwordHash)
RETURNING *;
