-- UserDevice table — Device-Binding pro User (Anti-Account-Sharing).
-- Backed by `model UserDevice` in schema.prisma.
--
-- Limits siehe plan-features.ts maxDevices: Free=1, Pro=1, Legend=3.
-- Frontend liefert deviceId via Capacitor Device.getId() (persistent UUID).
-- Auth-Middleware enforced via x-device-id Header.

CREATE TABLE IF NOT EXISTS "rebreak"."user_devices" (
    "id"           UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    "user_id"      UUID NOT NULL,
    "device_id"    TEXT NOT NULL,
    "platform"     TEXT NOT NULL,
    "model"        TEXT,
    "name"         TEXT,
    "last_seen_at" TIMESTAMP(3) WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at"   TIMESTAMP(3) WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX IF NOT EXISTS "user_devices_user_id_device_id_key"
    ON "rebreak"."user_devices"("user_id", "device_id");

CREATE INDEX IF NOT EXISTS "user_devices_user_id_idx"
    ON "rebreak"."user_devices"("user_id");
