-- Migration: add_protected_devices
-- Multi-Device DNS-Schutz für Legend-User.
-- Legend: bis zu 3 Geräte mit individuellem DoH-Token geschützt.

CREATE TABLE "rebreak"."protected_devices" (
    "id"               UUID         NOT NULL DEFAULT gen_random_uuid(),
    "user_id"          UUID         NOT NULL,
    "dns_token"        TEXT         NOT NULL,
    "platform"         TEXT         NOT NULL,
    "label"            TEXT         NOT NULL,
    "status"           TEXT         NOT NULL DEFAULT 'pending',
    "installed_at"     TIMESTAMPTZ,
    "last_dns_query_at" TIMESTAMPTZ,
    "revoked_at"       TIMESTAMPTZ,
    "created_at"       TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
    "updated_at"       TIMESTAMPTZ  NOT NULL DEFAULT NOW(),

    CONSTRAINT "protected_devices_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "protected_devices_dns_token_key" ON "rebreak"."protected_devices"("dns_token");
CREATE INDEX "protected_devices_user_id_idx"   ON "rebreak"."protected_devices"("user_id");
CREATE INDEX "protected_devices_dns_token_idx" ON "rebreak"."protected_devices"("dns_token");
