BEGIN;

CREATE TABLE IF NOT EXISTS "cpr_monitoring" (
  "cpr_monitoring_id" SERIAL PRIMARY KEY,
  "cpr_id" INTEGER NOT NULL,
  "cpr_monitoring_preview" BOOLEAN NOT NULL DEFAULT FALSE,
  "cpr_monitoring_description" TEXT NOT NULL,
  "cpr_monitoring_link" TEXT NOT NULL
);

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1
    FROM pg_constraint
    WHERE conname = 'cpr_monitoring_cpr_id_fkey'
  ) THEN
    ALTER TABLE "cpr_monitoring"
      ADD CONSTRAINT cpr_monitoring_cpr_id_fkey
      FOREIGN KEY ("cpr_id") REFERENCES "cpr"("cpr_id")
      ON DELETE CASCADE;
  END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_cpr_monitoring_cpr_id ON "cpr_monitoring"("cpr_id");

COMMIT;
