-- Migration 100: Web Push subscriptions + user notification flag
--
-- Tabela push_subscriptions: wielokrotne subskrypcje per user (desktop + mobile + PWA iOS).
-- Kolumna users.notify_push_messages analogiczna do notify_email_messages.

CREATE TABLE IF NOT EXISTS push_subscriptions (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    endpoint TEXT NOT NULL UNIQUE,
    p256dh VARCHAR(255) NOT NULL,
    auth VARCHAR(255) NOT NULL,
    user_agent VARCHAR(500),
    created_at TIMESTAMP DEFAULT NOW(),
    last_used_at TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_push_subscriptions_user_id ON push_subscriptions(user_id);

ALTER TABLE users ADD COLUMN IF NOT EXISTS notify_push_messages BOOLEAN DEFAULT TRUE;

GRANT ALL ON TABLE push_subscriptions TO nordabiz_app;
GRANT USAGE, SELECT ON SEQUENCE push_subscriptions_id_seq TO nordabiz_app;
