-- Schema: spider__cre_Doc_Template_Mgt
CREATE SCHEMA IF NOT EXISTS "spider__cre_Doc_Template_Mgt";

-- Table: Documents
CREATE TABLE "spider__cre_Doc_Template_Mgt"."Documents" (
    "Document_ID" BIGINT NOT NULL,
    "Template_ID" BIGINT,
    "Document_Name" TEXT,
    "Document_Description" TEXT,
    "Other_Details" TEXT,
    CONSTRAINT "Documents_pkey" PRIMARY KEY ("Document_ID")
);

-- Table: Paragraphs
CREATE TABLE "spider__cre_Doc_Template_Mgt"."Paragraphs" (
    "Paragraph_ID" BIGINT NOT NULL,
    "Document_ID" BIGINT NOT NULL,
    "Paragraph_Text" TEXT,
    "Other_Details" TEXT,
    CONSTRAINT "Paragraphs_pkey" PRIMARY KEY ("Paragraph_ID")
);

-- Table: Ref_Template_Types
CREATE TABLE "spider__cre_Doc_Template_Mgt"."Ref_Template_Types" (
    "Template_Type_Code" TEXT NOT NULL,
    "Template_Type_Description" TEXT NOT NULL,
    CONSTRAINT "Ref_Template_Types_pkey" PRIMARY KEY ("Template_Type_Code")
);

-- Table: Templates
CREATE TABLE "spider__cre_Doc_Template_Mgt"."Templates" (
    "Template_ID" BIGINT NOT NULL,
    "Version_Number" BIGINT NOT NULL,
    "Template_Type_Code" TEXT NOT NULL,
    "Date_Effective_From" TIMESTAMP,
    "Date_Effective_To" TIMESTAMP,
    "Template_Details" TEXT NOT NULL,
    CONSTRAINT "Templates_pkey" PRIMARY KEY ("Template_ID")
);

-- Foreign Keys
ALTER TABLE "spider__cre_Doc_Template_Mgt"."Documents" ADD CONSTRAINT "fk_Documents_0" FOREIGN KEY ("Template_ID") REFERENCES "spider__cre_Doc_Template_Mgt"."Templates" ("Template_ID") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__cre_Doc_Template_Mgt"."Paragraphs" ADD CONSTRAINT "fk_Paragraphs_0" FOREIGN KEY ("Document_ID") REFERENCES "spider__cre_Doc_Template_Mgt"."Documents" ("Document_ID") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "spider__cre_Doc_Template_Mgt"."Templates" ADD CONSTRAINT "fk_Templates_0" FOREIGN KEY ("Template_Type_Code") REFERENCES "spider__cre_Doc_Template_Mgt"."Ref_Template_Types" ("Template_Type_Code") ON DELETE NO ACTION ON UPDATE NO ACTION;
