-- Migration: 20260312_user_integrations
-- Drittanbieter-OAuth-Token-Speicher fuer User-Integrationen (z.B. Microsoft 365)

CREATE TABLE "user_integrations" (
  "id"            UUID          NOT NULL DEFAULT gen_random_uuid(),
  "user_id"       UUID          NOT NULL,
  "provider"      VARCHAR(50)   NOT NULL,
  "access_token"  TEXT          NOT NULL,
  "refresh_token" TEXT          NOT NULL,
  "expires_at"    TIMESTAMPTZ   NOT NULL,
  "scopes"        TEXT[]        NOT NULL DEFAULT '{}',
  "ms_tenant_id"  VARCHAR(100),
  "created_at"    TIMESTAMPTZ   NOT NULL DEFAULT now(),
  "updated_at"    TIMESTAMPTZ   NOT NULL DEFAULT now(),

  CONSTRAINT "user_integrations_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "user_integrations_user_id_provider_key" UNIQUE ("user_id", "provider"),
  CONSTRAINT "user_integrations_user_id_fkey"
    FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE
);

CREATE INDEX "user_integrations_user_id_idx" ON "user_integrations"("user_id");
