-- name: GetEventBySlug :one
SELECT * FROM events WHERE slug = ?;

-- name: GetEventByAdminToken :one
SELECT * FROM events WHERE admin_token = ?;

-- name: CreateEvent :one
INSERT INTO events (slug, title, date, time, location, admin_token, description)
VALUES (?, ?, ?, ?, ?, ?, ?)
RETURNING *;

-- name: UpdateEventDescription :exec
UPDATE events SET description = ? WHERE id = ?;

-- name: UpdateEvent :exec
UPDATE events SET title = ?, date = ?, time = ?, location = ? WHERE id = ?;

-- name: UpdateEventLocation :exec
UPDATE events SET location = ? WHERE id = ?;

-- name: DeleteEvent :exec
DELETE FROM events WHERE id = ?;

-- name: ListSlots :many
SELECT * FROM slots WHERE event_id = ? ORDER BY sort_order, id;

-- name: GetSlot :one
SELECT * FROM slots WHERE id = ?;

-- name: CreateSlot :one
INSERT INTO slots (event_id, name, emoji, max_claims, sort_order)
VALUES (?, ?, ?, ?, ?)
RETURNING *;

-- name: UpdateSlot :exec
UPDATE slots SET name = ?, emoji = ?, max_claims = ? WHERE id = ?;

-- name: DeleteSlot :exec
DELETE FROM slots WHERE id = ?;

-- name: ListClaimsBySlot :many
SELECT * FROM claims WHERE slot_id = ? ORDER BY created_at;

-- name: ListClaimsByEvent :many
SELECT c.* FROM claims c
JOIN slots s ON c.slot_id = s.id
WHERE s.event_id = ?
ORDER BY c.slot_id, c.created_at;

-- name: CreateClaim :one
INSERT INTO claims (slot_id, name, note)
VALUES (?, ?, ?)
RETURNING *;

-- name: GetClaimByID :one
SELECT * FROM claims WHERE id = ?;

-- name: DeleteClaim :exec
DELETE FROM claims WHERE id = ?;

-- name: CountClaimsBySlot :one
SELECT COUNT(*) FROM claims WHERE slot_id = ?;

-- name: ListRsvps :many
SELECT * FROM rsvps WHERE event_id = ? ORDER BY created_at;

-- name: CreateRsvp :one
INSERT INTO rsvps (event_id, name, note, plus_one)
VALUES (?, ?, ?, ?)
RETURNING *;

-- name: DeleteRsvp :exec
DELETE FROM rsvps WHERE id = ?;

-- name: CountRsvps :one
SELECT COUNT(*) FROM rsvps WHERE event_id = ?;

-- name: GetRsvpByName :one
SELECT * FROM rsvps WHERE event_id = ? AND name = ? COLLATE NOCASE LIMIT 1;

-- name: DeleteDuplicateRsvps :exec
DELETE FROM rsvps WHERE id NOT IN (
  SELECT MIN(id) FROM rsvps GROUP BY event_id, name COLLATE NOCASE
);

-- name: GetUserByPhone :one
SELECT * FROM users WHERE phone = ?;

-- name: GetUserByEmail :one
SELECT * FROM users WHERE email = ?;

-- name: CreateUserByPhone :one
INSERT INTO users (phone, name) VALUES (?, '') RETURNING *;

-- name: CreateUserByEmail :one
INSERT INTO users (email, name) VALUES (?, '') RETURNING *;

-- name: UpdateUserName :exec
UPDATE users SET name = ? WHERE id = ?;

-- name: CreateSession :exec
INSERT INTO sessions (token, user_id, expires_at) VALUES (?, ?, ?);

-- name: GetSession :one
SELECT * FROM sessions WHERE token = ? AND expires_at > CURRENT_TIMESTAMP;

-- name: DeleteSession :exec
DELETE FROM sessions WHERE token = ?;

-- name: DeleteExpiredSessions :exec
DELETE FROM sessions WHERE expires_at <= CURRENT_TIMESTAMP;

-- name: CreateVerificationCode :exec
INSERT INTO verification_codes (identifier, code, expires_at) VALUES (?, ?, ?);

-- name: GetVerificationCode :one
SELECT * FROM verification_codes
WHERE identifier = ? AND code = ? AND used = 0 AND expires_at > CURRENT_TIMESTAMP
ORDER BY id DESC LIMIT 1;

-- name: MarkVerificationCodeUsed :exec
UPDATE verification_codes SET used = 1 WHERE id = ?;

-- name: ListEventsByUser :many
SELECT * FROM events WHERE user_id = ? ORDER BY created_at DESC;

-- name: SetEventUser :exec
UPDATE events SET user_id = ? WHERE id = ?;

-- name: GetRsvp :one
SELECT * FROM rsvps WHERE id = ?;

-- name: UpdateRsvp :exec
UPDATE rsvps SET name = ?, note = ?, plus_one = ? WHERE id = ?;

-- name: CreateSmsConsent :exec
INSERT INTO sms_consents (phone, ip_address, user_agent) VALUES (?, ?, ?);
