-- +goose Up
-- +goose StatementBegin

-- Push notifications do PWA Gestor: assinaturas do navegador (Web Push API)
-- e controle de deduplicação de alertas críticos (evita notificar de novo
-- o mesmo problema em aberto a cada poucos minutos).

CREATE TABLE IF NOT EXISTS push_subscriptions (
    id          TEXT PRIMARY KEY,
    tenant_id   UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
    user_id     UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    endpoint    TEXT NOT NULL,
    p256dh      TEXT NOT NULL,
    auth        TEXT NOT NULL,
    user_agent  TEXT NOT NULL DEFAULT '',
    created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    UNIQUE (tenant_id, endpoint)
);

CREATE INDEX IF NOT EXISTS idx_push_subscriptions_tenant ON push_subscriptions(tenant_id);

CREATE TABLE IF NOT EXISTS gestor_alerts_sent (
    tenant_id    UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
    alert_key    TEXT NOT NULL,
    sent_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (tenant_id, alert_key)
);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS gestor_alerts_sent;
DROP TABLE IF EXISTS push_subscriptions;
-- +goose StatementEnd
