CREATE TABLE "notification_push_subscriptions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" bigint NOT NULL,
	"user_id" uuid NOT NULL,
	"endpoint" text NOT NULL,
	"p256dh" text NOT NULL,
	"auth" text NOT NULL,
	"user_agent" text,
	"device_label" text,
	"meta" jsonb,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"last_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
	"disabled_at" timestamp with time zone,
	CONSTRAINT "notification_push_subscriptions_endpoint_key" UNIQUE("endpoint")
);

ALTER TABLE "notification_push_subscriptions"
	ADD CONSTRAINT "notification_push_subscriptions_tenant_id_tenants_id_fk"
	FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id")
	ON DELETE cascade ON UPDATE cascade;

ALTER TABLE "notification_push_subscriptions"
	ADD CONSTRAINT "notification_push_subscriptions_user_id_auth_users_id_fk"
	FOREIGN KEY ("user_id") REFERENCES "public"."auth_users"("id")
	ON DELETE cascade ON UPDATE cascade;

INSERT INTO "notifications_event_types" (
	"event_key",
	"display_name",
	"description",
	"category",
	"severity",
	"allowed_channels"
) VALUES
	('system.test_push', 'Test-Push', 'Testet Desktop-Benachrichtigungen für den angemeldeten Nutzer.', 'system', 'info', '["inapp", "push"]'::jsonb),
	('communication.message.new', 'Neue Chatnachricht', 'Benachrichtigt über relevante neue Chatnachrichten.', 'communication', 'info', '["inapp", "push"]'::jsonb),
	('communication.call.started', 'Besprechung gestartet', 'Benachrichtigt Raumteilnehmer über gestartete Audio- oder Videoanrufe.', 'communication', 'info', '["inapp", "push"]'::jsonb),
	('communication.call.missed', 'Verpasster Anruf', 'Benachrichtigt über verpasste Besprechungen.', 'communication', 'warning', '["inapp", "push"]'::jsonb),
	('communication.room.invited', 'Raumeinladung', 'Benachrichtigt über Einladungen in Kommunikationsräume.', 'communication', 'info', '["inapp", "push"]'::jsonb)
ON CONFLICT ("event_key") DO UPDATE SET
	"display_name" = EXCLUDED."display_name",
	"description" = EXCLUDED."description",
	"category" = EXCLUDED."category",
	"severity" = EXCLUDED."severity",
	"allowed_channels" = EXCLUDED."allowed_channels",
	"is_active" = true;
