-- name: CreateUser :exec
INSERT INTO users (id, referrer_id, points)
VALUES (?, ?, 0);

-- name: GetUser :one
SELECT id, points, referrer_id FROM users
WHERE id = ?;

-- name: AddPointsToUser :exec
UPDATE users
SET points = points + ?
WHERE id = ?;

-- name: ReducePointsFromUser :one
UPDATE users
SET points = points - ?
WHERE id = ?
RETURNING points;

-- name: UpsertReferrer :exec
INSERT INTO users (id, referrer_id, points)
VALUES (?, ?, 0)
ON CONFLICT(id) DO UPDATE SET
    referrer_id = excluded.referrer_id
WHERE users.referrer_id IS NULL;

-- name: UpdateReferrer :exec
UPDATE users
SET referrer_id = ?
WHERE id = ? AND referrer_id IS NULL;
