-- CreateTable
CREATE TABLE "crypto_entries" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "dienst" TEXT NOT NULL,
    "schluessel" TEXT NOT NULL,
    "algorithmus" TEXT,
    "ablauf_datum" TIMESTAMP(3),
    "verantwortlich" TEXT NOT NULL,
    "speicherort" TEXT,
    "baseline_ref" TEXT,
    "notes" TEXT,
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "classification_classes" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "handling_aspects" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "category" TEXT,
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "handling_rules" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "class_id" TEXT NOT NULL,
    "aspect_id" TEXT NOT NULL,
    "text" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "risk_matrix_classes" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "max_score" INTEGER NOT NULL,
    "acceptance" TEXT NOT NULL,
    "tone" TEXT NOT NULL,
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "risk_ew_levels" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "level" INTEGER NOT NULL,
    "label" TEXT NOT NULL,
    "definition" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "risk_damage_dimensions" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "levels" JSONB NOT NULL,
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "handbook_topics" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "body_md" TEXT NOT NULL,
    "source_refs" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "crypto_entries_tenant_id_idx" ON "crypto_entries"("tenant_id");

-- CreateIndex
CREATE INDEX "classification_classes_tenant_id_idx" ON "classification_classes"("tenant_id");

-- CreateIndex
CREATE INDEX "handling_aspects_tenant_id_idx" ON "handling_aspects"("tenant_id");

-- CreateIndex
CREATE INDEX "handling_rules_tenant_id_idx" ON "handling_rules"("tenant_id");

-- CreateIndex
CREATE UNIQUE INDEX "handling_rules_class_id_aspect_id_key" ON "handling_rules"("class_id", "aspect_id");

-- CreateIndex
CREATE INDEX "risk_matrix_classes_tenant_id_idx" ON "risk_matrix_classes"("tenant_id");

-- CreateIndex
CREATE INDEX "risk_ew_levels_tenant_id_idx" ON "risk_ew_levels"("tenant_id");

-- CreateIndex
CREATE INDEX "risk_damage_dimensions_tenant_id_idx" ON "risk_damage_dimensions"("tenant_id");

-- CreateIndex
CREATE INDEX "handbook_topics_tenant_id_idx" ON "handbook_topics"("tenant_id");

-- AddForeignKey
ALTER TABLE "handling_rules" ADD CONSTRAINT "handling_rules_class_id_fkey" FOREIGN KEY ("class_id") REFERENCES "classification_classes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "handling_rules" ADD CONSTRAINT "handling_rules_aspect_id_fkey" FOREIGN KEY ("aspect_id") REFERENCES "handling_aspects"("id") ON DELETE CASCADE ON UPDATE CASCADE;


-- RLS-Policies für die verwalteten Register-Tabellen
DO $$
DECLARE t text;
BEGIN
  FOREACH t IN ARRAY ARRAY[
    'crypto_entries','classification_classes','handling_aspects','handling_rules',
    'risk_matrix_classes','risk_ew_levels','risk_damage_dimensions','handbook_topics'
  ] LOOP
    EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', t);
    EXECUTE format(
      'CREATE POLICY tenant_isolation ON %I USING (tenant_id = current_setting(''app.tenant_id'', true))',
      t
    );
  END LOOP;
END $$;
