CREATE TABLE "tblAssemblies" (
	"Id" UUID NOT NULL UNIQUE,
	"Name" VARCHAR(255) NOT NULL,
	"Price" MONEY NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);


CREATE TABLE "tblUsers" (
	"Id" UUID NOT NULL UNIQUE,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);


CREATE TABLE "tblMaterials" (
	"Id" UUID NOT NULL UNIQUE,
	"Name" VARCHAR(255),
	"TypeId" SERIAL NOT NULL,
	"ColorId" SERIAL,
	"ManufacturerName" VARCHAR(255),
	"StoreLinkId" UUID,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblPrinters" (
	"Id" UUID NOT NULL UNIQUE,
	"Name" VARCHAR(255) NOT NULL,
	"WorkTimeHours" INTEGER NOT NULL,
	"DetailsPrinted" INTEGER,
	"ConnectionUrl" VARCHAR(255),
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblPrintedDetails" (
	"Id" UUID NOT NULL UNIQUE,
	"ParentDetailId" UUID,
	"Name" VARCHAR(255),
	"MaterialId" UUID NOT NULL,
	"GCodeId" UUID NOT NULL,
	"Weight" REAL NOT NULL,
	"HasVariants" BIT(1) NOT NULL DEFAULT 0,
	"Price" MONEY NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblComponents" (
	"Id" UUID NOT NULL UNIQUE,
	"Name" VARCHAR(255) NOT NULL,
	"TypeId" INTEGER NOT NULL,
	"StoreLinkId" UUID NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblOrders" (
	"Id" UUID NOT NULL UNIQUE,
	"ClientId" UUID NOT NULL,
	"Price" MONEY NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblOrderItems" (
	"Id" UUID NOT NULL UNIQUE,
	"AssemblyId" UUID NOT NULL,
	"Count" INTEGER NOT NULL,
	"OrderId" UUID NOT NULL,
	"Price" MONEY,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblPrintedDetailsAssemblies" (
	"Id" UUID NOT NULL UNIQUE,
	"PrintedDetailId" UUID NOT NULL,
	"AssemblyId" UUID NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblAssembliesComponents" (
	"Id" UUID NOT NULL UNIQUE,
	"AssemblyId" UUID NOT NULL,
	"ComponentId" UUID NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblGCodes" (
	"Id" UUID NOT NULL UNIQUE,
	"Name" VARCHAR(255) NOT NULL,
	"StoragePath" VARCHAR(255) NOT NULL,
	"PrinterId" UUID NOT NULL,
	"MaterialId" UUID,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblMaterialTypes" (
	"Id" SERIAL NOT NULL UNIQUE,
	"Name" VARCHAR(255) NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblMaterialColors" (
	"Id" SERIAL NOT NULL UNIQUE,
	"Name" VARCHAR(255) NOT NULL,
	"HEX" CHAR(7) NOT NULL UNIQUE,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblComponentsTypes" (
	"Id" SERIAL NOT NULL UNIQUE,
	"Name" VARCHAR(255) NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblStoreLink" (
	"Id" UUID NOT NULL UNIQUE,
	"Url" VARCHAR(255) NOT NULL,
	"Price" MONEY NOT NULL,
	"ItemId" UUID NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);




CREATE TABLE "tblPrintJob" (
	"Id" UUID NOT NULL UNIQUE,
	"Name" VARCHAR(255) NOT NULL,
	"GCodeId" UUID NOT NULL,
	"StartDate" DATE NOT NULL,
	"EndDate" DATE NOT NULL,
	"Success" BIT(1) NOT NULL,
	"CreatedAt" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
	"CreatedBy" UUID,
	"Enabled" BOOLEAN NOT NULL DEFAULT TRUE,
	PRIMARY KEY("Id")
);



ALTER TABLE "tblPrintedDetailsAssemblies"
ADD FOREIGN KEY("PrintedDetailId") REFERENCES "tblPrintedDetails"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrintedDetailsAssemblies"
ADD FOREIGN KEY("AssemblyId") REFERENCES "tblAssemblies"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblAssemblies"
ADD FOREIGN KEY("Id") REFERENCES "tblAssembliesComponents"("AssemblyId")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblComponents"
ADD FOREIGN KEY("Id") REFERENCES "tblAssembliesComponents"("ComponentId")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblAssemblies"
ADD FOREIGN KEY("Id") REFERENCES "tblOrderItems"("AssemblyId")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblOrderItems"
ADD FOREIGN KEY("OrderId") REFERENCES "tblOrders"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblOrders"
ADD FOREIGN KEY("Id") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrinters"
ADD FOREIGN KEY("Id") REFERENCES "tblGCodes"("PrinterId")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblMaterials"
ADD FOREIGN KEY("Id") REFERENCES "tblGCodes"("MaterialId")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrintedDetails"
ADD FOREIGN KEY("GCodeId") REFERENCES "tblGCodes"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblMaterials"
ADD FOREIGN KEY("TypeId") REFERENCES "tblMaterialTypes"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblMaterials"
ADD FOREIGN KEY("ColorId") REFERENCES "tblMaterialColors"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblComponents"
ADD FOREIGN KEY("TypeId") REFERENCES "tblComponentsTypes"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrintedDetails"
ADD FOREIGN KEY("ParrentDetailId") REFERENCES "tblPrintedDetails"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblStoreLink"
ADD FOREIGN KEY("ItemId") REFERENCES "tblComponents"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblStoreLink"
ADD FOREIGN KEY("ItemId") REFERENCES "tblMaterials"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrintJob"
ADD FOREIGN KEY("GCodeId") REFERENCES "tblGCodes"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblAssemblies"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblUsers"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblMaterials"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrinters"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrintedDetails"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblComponents"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblOrders"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblOrderItems"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrintedDetailsAssemblies"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblAssembliesComponents"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblGCodes"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblMaterialTypes"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblMaterialColors"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblComponentsTypes"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblStoreLink"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;
ALTER TABLE "tblPrintJob"
ADD FOREIGN KEY("CreatedBy") REFERENCES "tblUsers"("Id")
ON UPDATE NO ACTION ON DELETE NO ACTION;