-- push_subscriptions: Web Push endpoints for browsers / PWAs. id is the hex
-- SHA-256 of the endpoint, so a re-subscribe from the same browser upserts in
-- place rather than piling up duplicate rows. Lookups are by (user_id,
-- device_name): the receiving device of a notify-worthy event. Dead endpoints
-- (push service returns 404/410) are pruned by DeletePushSubscription.
-- NOTE: keep this file pure ASCII; sqlc v1.31.1 drifts byte offsets on
-- multibyte runes in query files, corrupting the generated SQL.

-- name: UpsertPushSubscription :exec
INSERT INTO push_subscriptions (id, user_id, device_name, endpoint, p256dh, auth, user_agent, locale, created_at, last_used_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
    user_id      = excluded.user_id,
    device_name  = excluded.device_name,
    p256dh       = excluded.p256dh,
    auth         = excluded.auth,
    user_agent   = excluded.user_agent,
    locale       = excluded.locale,
    last_used_at = excluded.last_used_at;

-- name: ListPushSubscriptionsByUserDevice :many
SELECT id, user_id, device_name, endpoint, p256dh, auth, user_agent, locale, created_at, last_used_at
FROM push_subscriptions
WHERE user_id = ? AND device_name = ?;

-- name: TouchPushSubscription :exec
UPDATE push_subscriptions
SET last_used_at = ?
WHERE id = ?;

-- name: DeletePushSubscription :exec
DELETE FROM push_subscriptions
WHERE id = ?;

-- name: DeletePushSubscriptionsByUserDevice :exec
DELETE FROM push_subscriptions
WHERE user_id = ? AND device_name = ?;
