CREATE TABLE "Currencies" (
    id uuid NOT NULL,
    "ISOCode" text NOT NULL,
    CONSTRAINT "PK_Currencies" PRIMARY KEY (id)
);


CREATE TABLE "FormQuestion" (
    id uuid NOT NULL,
    "Order" integer NOT NULL,
    CONSTRAINT "PK_FormQuestion" PRIMARY KEY (id)
);


CREATE TABLE "Languages" (
    id uuid NOT NULL,
    "ISOCode" text NOT NULL,
    CONSTRAINT "PK_Languages" PRIMARY KEY (id)
);


CREATE TABLE "ProductGroups" (
    id uuid NOT NULL,
    CONSTRAINT "PK_ProductGroups" PRIMARY KEY (id)
);


CREATE TABLE "UserGroups" (
    id uuid NOT NULL,
    name text NOT NULL,
    CONSTRAINT "PK_UserGroups" PRIMARY KEY (id)
);


CREATE TABLE "FormQuestionsAnswer" (
    id uuid NOT NULL,
    "formQuestionid" uuid NOT NULL,
    "order" integer NOT NULL,
    CONSTRAINT "PK_FormQuestionsAnswer" PRIMARY KEY (id),
    CONSTRAINT "FK_FormQuestionsAnswer_FormQuestion_formQuestionid" FOREIGN KEY ("formQuestionid") REFERENCES "FormQuestion" (id) ON DELETE CASCADE
);


CREATE TABLE "CurrencyLanguages" (
    id uuid NOT NULL,
    currencyid uuid NOT NULL,
    languageid uuid NOT NULL,
    name text NOT NULL,
    CONSTRAINT "PK_CurrencyLanguages" PRIMARY KEY (id),
    CONSTRAINT "FK_CurrencyLanguages_Currencies_currencyid" FOREIGN KEY (currencyid) REFERENCES "Currencies" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_CurrencyLanguages_Languages_languageid" FOREIGN KEY (languageid) REFERENCES "Languages" (id) ON DELETE CASCADE
);


CREATE TABLE "FormQuestionsLanguage" (
    id uuid NOT NULL,
    questionid uuid NOT NULL,
    languageid uuid NOT NULL,
    text text NOT NULL,
    CONSTRAINT "PK_FormQuestionsLanguage" PRIMARY KEY (id),
    CONSTRAINT "FK_FormQuestionsLanguage_FormQuestion_questionid" FOREIGN KEY (questionid) REFERENCES "FormQuestion" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_FormQuestionsLanguage_Languages_languageid" FOREIGN KEY (languageid) REFERENCES "Languages" (id) ON DELETE CASCADE
);


CREATE TABLE "LanguageLanguages" (
    id uuid NOT NULL,
    names uuid NOT NULL,
    "usedToTranlateLanguage" uuid NOT NULL,
    name text NOT NULL,
    CONSTRAINT "PK_LanguageLanguages" PRIMARY KEY (id),
    CONSTRAINT "FK_LanguageLanguages_Languages_names" FOREIGN KEY (names) REFERENCES "Languages" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_LanguageLanguages_Languages_usedToTranlateLanguage" FOREIGN KEY ("usedToTranlateLanguage") REFERENCES "Languages" (id) ON DELETE CASCADE
);


CREATE TABLE "Users" (
    id uuid NOT NULL,
    email text NOT NULL,
    password text NOT NULL,
    "favoriteLanguageid" uuid NOT NULL,
    "creationDate" timestamp without time zone NOT NULL,
    hash bytea NOT NULL,
    CONSTRAINT "PK_Users" PRIMARY KEY (id),
    CONSTRAINT "FK_Users_Languages_favoriteLanguageid" FOREIGN KEY ("favoriteLanguageid") REFERENCES "Languages" (id) ON DELETE CASCADE
);


CREATE TABLE "ProductGroupLanguages" (
    id uuid NOT NULL,
    productgroupid uuid NOT NULL,
    languageid uuid NOT NULL,
    name text NOT NULL,
    CONSTRAINT "PK_ProductGroupLanguages" PRIMARY KEY (id),
    CONSTRAINT "FK_ProductGroupLanguages_Languages_languageid" FOREIGN KEY (languageid) REFERENCES "Languages" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_ProductGroupLanguages_ProductGroups_productgroupid" FOREIGN KEY (productgroupid) REFERENCES "ProductGroups" (id) ON DELETE CASCADE
);


CREATE TABLE "ProductSubGroups" (
    id uuid NOT NULL,
    "Groupeid" uuid NOT NULL,
    CONSTRAINT "PK_ProductSubGroups" PRIMARY KEY (id),
    CONSTRAINT "FK_ProductSubGroups_ProductGroups_Groupeid" FOREIGN KEY ("Groupeid") REFERENCES "ProductGroups" (id) ON DELETE CASCADE
);


CREATE TABLE "FormQuestionsAnswerLanguage" (
    id uuid NOT NULL,
    answerid uuid NOT NULL,
    languageid uuid NOT NULL,
    text text NOT NULL,
    CONSTRAINT "PK_FormQuestionsAnswerLanguage" PRIMARY KEY (id),
    CONSTRAINT "FK_FormQuestionsAnswerLanguage_FormQuestionsAnswer_answerid" FOREIGN KEY (answerid) REFERENCES "FormQuestionsAnswer" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_FormQuestionsAnswerLanguage_Languages_languageid" FOREIGN KEY (languageid) REFERENCES "Languages" (id) ON DELETE CASCADE
);


CREATE TABLE "FormQuestionsAnswerUser" (
    id uuid NOT NULL,
    answerid uuid NOT NULL,
    userid uuid NOT NULL,
    CONSTRAINT "PK_FormQuestionsAnswerUser" PRIMARY KEY (id),
    CONSTRAINT "FK_FormQuestionsAnswerUser_FormQuestionsAnswer_answerid" FOREIGN KEY (answerid) REFERENCES "FormQuestionsAnswer" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_FormQuestionsAnswerUser_Users_userid" FOREIGN KEY (userid) REFERENCES "Users" (id) ON DELETE CASCADE
);


CREATE TABLE "Purchases" (
    id uuid NOT NULL,
    "datePurchase" timestamp without time zone NOT NULL,
    userid uuid NOT NULL,
    "initialPurchaseid" uuid,
    CONSTRAINT "PK_Purchases" PRIMARY KEY (id),
    CONSTRAINT "FK_Purchases_Purchases_initialPurchaseid" FOREIGN KEY ("initialPurchaseid") REFERENCES "Purchases" (id),
    CONSTRAINT "FK_Purchases_Users_userid" FOREIGN KEY (userid) REFERENCES "Users" (id) ON DELETE CASCADE
);


CREATE TABLE "UserInGroup" (
    id uuid NOT NULL,
    userid uuid NOT NULL,
    "userGroupid" uuid NOT NULL,
    CONSTRAINT "PK_UserInGroup" PRIMARY KEY (id),
    CONSTRAINT "FK_UserInGroup_UserGroups_userGroupid" FOREIGN KEY ("userGroupid") REFERENCES "UserGroups" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_UserInGroup_Users_userid" FOREIGN KEY (userid) REFERENCES "Users" (id) ON DELETE CASCADE
);


CREATE TABLE "Products" (
    id uuid NOT NULL,
    subgroupid uuid NOT NULL,
    "CO2" double precision NOT NULL,
    "CO2Unit" text NOT NULL,
    water double precision NOT NULL,
    "waterUnit" text NOT NULL,
    amortization integer NOT NULL,
    "Discriminator" character varying(8) NOT NULL,
    ciqual text,
    CONSTRAINT "PK_Products" PRIMARY KEY (id),
    CONSTRAINT "FK_Products_ProductSubGroups_subgroupid" FOREIGN KEY (subgroupid) REFERENCES "ProductSubGroups" (id) ON DELETE CASCADE
);


CREATE TABLE "ProductSubGroupLanguages" (
    id uuid NOT NULL,
    languageid uuid NOT NULL,
    name text NOT NULL,
    "ProductSubGroupid" uuid,
    CONSTRAINT "PK_ProductSubGroupLanguages" PRIMARY KEY (id),
    CONSTRAINT "FK_ProductSubGroupLanguages_Languages_languageid" FOREIGN KEY (languageid) REFERENCES "Languages" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_ProductSubGroupLanguages_ProductSubGroups_ProductSubGroupid" FOREIGN KEY ("ProductSubGroupid") REFERENCES "ProductSubGroups" (id)
);


CREATE TABLE "ProductLanguages" (
    id uuid NOT NULL,
    languageid uuid NOT NULL,
    name text NOT NULL,
    "Productid" uuid,
    CONSTRAINT "PK_ProductLanguages" PRIMARY KEY (id),
    CONSTRAINT "FK_ProductLanguages_Languages_languageid" FOREIGN KEY (languageid) REFERENCES "Languages" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_ProductLanguages_Products_Productid" FOREIGN KEY ("Productid") REFERENCES "Products" (id)
);


CREATE TABLE "ProductPurchases" (
    id uuid NOT NULL,
    purchaseid uuid NOT NULL,
    productid uuid NOT NULL,
    currencyid uuid NOT NULL,
    price double precision NOT NULL,
    quantity double precision NOT NULL,
    "CO2Cost" double precision NOT NULL,
    CONSTRAINT "PK_ProductPurchases" PRIMARY KEY (id),
    CONSTRAINT "FK_ProductPurchases_Currencies_currencyid" FOREIGN KEY (currencyid) REFERENCES "Currencies" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_ProductPurchases_Products_productid" FOREIGN KEY (productid) REFERENCES "Products" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_ProductPurchases_Purchases_purchaseid" FOREIGN KEY (purchaseid) REFERENCES "Purchases" (id) ON DELETE CASCADE
);


CREATE TABLE "ProductUserTranslations" (
    id uuid NOT NULL,
    userid uuid NOT NULL,
    productid uuid NOT NULL,
    name text NOT NULL,
    CONSTRAINT "PK_ProductUserTranslations" PRIMARY KEY (id),
    CONSTRAINT "FK_ProductUserTranslations_Products_productid" FOREIGN KEY (productid) REFERENCES "Products" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_ProductUserTranslations_Users_userid" FOREIGN KEY (userid) REFERENCES "Users" (id) ON DELETE CASCADE
);


CREATE TABLE "CarbonLoans" (
    id uuid NOT NULL,
    userid uuid NOT NULL,
    "productPurchaseid" uuid NOT NULL,
    "dateBegin" timestamp without time zone NOT NULL,
    "dateEnd" timestamp without time zone NOT NULL,
    CONSTRAINT "PK_CarbonLoans" PRIMARY KEY (id),
    CONSTRAINT "FK_CarbonLoans_ProductPurchases_productPurchaseid" FOREIGN KEY ("productPurchaseid") REFERENCES "ProductPurchases" (id) ON DELETE CASCADE,
    CONSTRAINT "FK_CarbonLoans_Users_userid" FOREIGN KEY (userid) REFERENCES "Users" (id) ON DELETE CASCADE
);


CREATE INDEX "IX_CarbonLoans_productPurchaseid" ON "CarbonLoans" ("productPurchaseid");


CREATE INDEX "IX_CarbonLoans_userid" ON "CarbonLoans" (userid);


CREATE INDEX "IX_CurrencyLanguages_currencyid" ON "CurrencyLanguages" (currencyid);


CREATE INDEX "IX_CurrencyLanguages_languageid" ON "CurrencyLanguages" (languageid);


CREATE INDEX "IX_FormQuestionsAnswer_formQuestionid" ON "FormQuestionsAnswer" ("formQuestionid");


CREATE INDEX "IX_FormQuestionsAnswerLanguage_answerid" ON "FormQuestionsAnswerLanguage" (answerid);


CREATE INDEX "IX_FormQuestionsAnswerLanguage_languageid" ON "FormQuestionsAnswerLanguage" (languageid);


CREATE INDEX "IX_FormQuestionsAnswerUser_answerid" ON "FormQuestionsAnswerUser" (answerid);


CREATE INDEX "IX_FormQuestionsAnswerUser_userid" ON "FormQuestionsAnswerUser" (userid);


CREATE INDEX "IX_FormQuestionsLanguage_languageid" ON "FormQuestionsLanguage" (languageid);


CREATE INDEX "IX_FormQuestionsLanguage_questionid" ON "FormQuestionsLanguage" (questionid);


CREATE INDEX "IX_LanguageLanguages_names" ON "LanguageLanguages" (names);


CREATE INDEX "IX_LanguageLanguages_usedToTranlateLanguage" ON "LanguageLanguages" ("usedToTranlateLanguage");


CREATE INDEX "IX_ProductGroupLanguages_languageid" ON "ProductGroupLanguages" (languageid);


CREATE INDEX "IX_ProductGroupLanguages_productgroupid" ON "ProductGroupLanguages" (productgroupid);


CREATE INDEX "IX_ProductLanguages_languageid" ON "ProductLanguages" (languageid);


CREATE INDEX "IX_ProductLanguages_Productid" ON "ProductLanguages" ("Productid");


CREATE INDEX "IX_ProductPurchases_currencyid" ON "ProductPurchases" (currencyid);


CREATE INDEX "IX_ProductPurchases_productid" ON "ProductPurchases" (productid);


CREATE INDEX "IX_ProductPurchases_purchaseid" ON "ProductPurchases" (purchaseid);


CREATE INDEX "IX_Products_subgroupid" ON "Products" (subgroupid);


CREATE INDEX "IX_ProductSubGroupLanguages_languageid" ON "ProductSubGroupLanguages" (languageid);


CREATE INDEX "IX_ProductSubGroupLanguages_ProductSubGroupid" ON "ProductSubGroupLanguages" ("ProductSubGroupid");


CREATE INDEX "IX_ProductSubGroups_Groupeid" ON "ProductSubGroups" ("Groupeid");


CREATE INDEX "IX_ProductUserTranslations_productid" ON "ProductUserTranslations" (productid);


CREATE INDEX "IX_ProductUserTranslations_userid" ON "ProductUserTranslations" (userid);


CREATE INDEX "IX_Purchases_initialPurchaseid" ON "Purchases" ("initialPurchaseid");


CREATE INDEX "IX_Purchases_userid" ON "Purchases" (userid);


CREATE INDEX "IX_UserInGroup_userGroupid" ON "UserInGroup" ("userGroupid");


CREATE INDEX "IX_UserInGroup_userid" ON "UserInGroup" (userid);


CREATE INDEX "IX_Users_favoriteLanguageid" ON "Users" ("favoriteLanguageid");