-- CreateEnum
CREATE TYPE "PolicyDocType" AS ENUM ('LEITLINIE', 'RICHTLINIE', 'VERFAHREN', 'REGISTER', 'HANDBUCH', 'EIGENES');

-- CreateEnum
CREATE TYPE "PolicyStatus" AS ENUM ('ENTWURF', 'IN_FREIGABE', 'FREIGEGEBEN', 'ARCHIVIERT');

-- CreateTable
CREATE TABLE "policy_documents" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "type" "PolicyDocType" NOT NULL,
    "title" TEXT NOT NULL,
    "version" TEXT NOT NULL DEFAULT '1.0',
    "status" "PolicyStatus" NOT NULL DEFAULT 'FREIGEGEBEN',
    "owner" TEXT,
    "policy_code" TEXT,
    "fulfills" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "raw_markdown" TEXT NOT NULL,
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "policy_requirements" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "req_id" TEXT NOT NULL,
    "policy_code" TEXT NOT NULL,
    "control" TEXT NOT NULL,
    "obligation" TEXT NOT NULL,
    "condition" TEXT,
    "requirement" TEXT NOT NULL,
    "implementation" TEXT NOT NULL,
    "va_codes" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "nachweis_link" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "policy_variables" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "kind" TEXT NOT NULL,
    "group_name" TEXT,
    "value" TEXT NOT NULL,
    "required" BOOLEAN NOT NULL DEFAULT false,
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "policy_baseline_params" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "bl_id" TEXT NOT NULL,
    "section" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "vorgabe" TEXT NOT NULL,
    "order_idx" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "policy_evidence" (
    "id" TEXT NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "nr" INTEGER NOT NULL,
    "policy_code" TEXT NOT NULL,
    "nachweis" TEXT NOT NULL,
    "quelle" TEXT NOT NULL,
    "verantwortlich" TEXT NOT NULL,
    "turnus" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "policy_documents_tenant_id_idx" ON "policy_documents"("tenant_id");

-- CreateIndex
CREATE UNIQUE INDEX "policy_documents_tenant_id_code_key" ON "policy_documents"("tenant_id", "code");

-- CreateIndex
CREATE INDEX "policy_requirements_tenant_id_idx" ON "policy_requirements"("tenant_id");

-- CreateIndex
CREATE INDEX "policy_requirements_tenant_id_control_idx" ON "policy_requirements"("tenant_id", "control");

-- CreateIndex
CREATE UNIQUE INDEX "policy_requirements_tenant_id_req_id_key" ON "policy_requirements"("tenant_id", "req_id");

-- CreateIndex
CREATE INDEX "policy_variables_tenant_id_idx" ON "policy_variables"("tenant_id");

-- CreateIndex
CREATE UNIQUE INDEX "policy_variables_tenant_id_key_key" ON "policy_variables"("tenant_id", "key");

-- CreateIndex
CREATE INDEX "policy_baseline_params_tenant_id_idx" ON "policy_baseline_params"("tenant_id");

-- CreateIndex
CREATE UNIQUE INDEX "policy_baseline_params_tenant_id_bl_id_key" ON "policy_baseline_params"("tenant_id", "bl_id");

-- CreateIndex
CREATE INDEX "policy_evidence_tenant_id_idx" ON "policy_evidence"("tenant_id");


-- RLS-Policies für die Richtlinien-Tabellen
DO $$
DECLARE t text;
BEGIN
  FOREACH t IN ARRAY ARRAY[
    'policy_documents','policy_requirements','policy_variables',
    'policy_baseline_params','policy_evidence'
  ] 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 $$;
