-- Create "DiagnoseSrc" table
CREATE TABLE "public"."DiagnoseSrc" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(2048) NULL,
  "IndName" character varying(2048) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_DiagnoseSrc_Code" UNIQUE ("Code")
);
-- Create "PharmacyCompany" table
CREATE TABLE "public"."PharmacyCompany" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(20) NULL,
  "Name" character varying(100) NULL,
  "Regency_Code" character varying(4) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_PharmacyCompany_Code" UNIQUE ("Code")
);
-- Create "Uom" table
CREATE TABLE "public"."Uom" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Uom_Code" UNIQUE ("Code")
);
-- Create "Counter" table
CREATE TABLE "public"."Counter" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(30) NULL,
  "Number" smallint NULL,
  "Parent_Id" integer NULL,
  "Type_Code" text NULL,
  "Queue_Code" character varying(5) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Counter_Code" UNIQUE ("Code")
);
-- Create "Item" table
CREATE TABLE "public"."Item" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(50) NULL,
  "Name" character varying(100) NULL,
  "ItemGroup_Code" character varying(10) NULL,
  "Uom_Code" character varying(10) NULL,
  "Infra_Id" integer NULL,
  "Stock" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Item_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Item_Uom" FOREIGN KEY ("Uom_Code") REFERENCES "public"."Uom" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Infra" table
CREATE TABLE "public"."Infra" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  "InfraGroup_Code" character varying(10) NULL,
  "Parent_Id" integer NULL,
  "Item_Id" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Infra_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Infra_Item" FOREIGN KEY ("Item_Id") REFERENCES "public"."Item" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Device" table
CREATE TABLE "public"."Device" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NOT NULL,
  "Uom_Code" character varying(10) NULL,
  "Infra_Id" integer NULL,
  "Item_Id" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Device_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Device_Infra" FOREIGN KEY ("Infra_Id") REFERENCES "public"."Infra" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Device_Item" FOREIGN KEY ("Item_Id") REFERENCES "public"."Item" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Device_Uom" FOREIGN KEY ("Uom_Code") REFERENCES "public"."Uom" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Province" table
CREATE TABLE "public"."Province" (
  "Id" smallserial NOT NULL,
  "Code" character varying(2) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Province_Code" UNIQUE ("Code")
);
-- Create "Regency" table
CREATE TABLE "public"."Regency" (
  "Id" serial NOT NULL,
  "Province_Code" character varying(2) NULL,
  "Code" character varying(4) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Regency_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Province_Regencies" FOREIGN KEY ("Province_Code") REFERENCES "public"."Province" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "District" table
CREATE TABLE "public"."District" (
  "Id" bigserial NOT NULL,
  "Regency_Code" character varying(4) NULL,
  "Code" character varying(6) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_District_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Regency_Districts" FOREIGN KEY ("Regency_Code") REFERENCES "public"."Regency" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Division" table
CREATE TABLE "public"."Division" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  "Parent_Id" smallint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Division_Code" UNIQUE ("Code")
);
-- Create "DivisionPosition" table
CREATE TABLE "public"."DivisionPosition" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Division_Id" integer NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_DivisionPosition_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_DivisionPosition_Division" FOREIGN KEY ("Division_Id") REFERENCES "public"."Division" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Ethnic" table
CREATE TABLE "public"."Ethnic" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(20) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Ethnic_Code" UNIQUE ("Code")
);
-- Create "Language" table
CREATE TABLE "public"."Language" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Language_Code" UNIQUE ("Code")
);
-- Create "Person" table
CREATE TABLE "public"."Person" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Name" character varying(150) NOT NULL,
  "FrontTitle" character varying(50) NULL,
  "EndTitle" character varying(50) NULL,
  "BirthDate" timestamptz NULL,
  "BirthRegency_Code" character varying(4) NULL,
  "Gender_Code" character varying(10) NULL,
  "ResidentIdentityNumber" character varying(16) NULL,
  "PassportNumber" character varying(20) NULL,
  "DrivingLicenseNumber" character varying(20) NULL,
  "Religion_Code" character varying(10) NULL,
  "Education_Code" character varying(10) NULL,
  "Ocupation_Code" character varying(15) NULL,
  "Ocupation_Name" character varying(50) NULL,
  "Ethnic_Code" character varying(20) NULL,
  "Language_Code" character varying(10) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Person_DrivingLicenseNumber" UNIQUE ("DrivingLicenseNumber"),
  CONSTRAINT "uni_Person_PassportNumber" UNIQUE ("PassportNumber"),
  CONSTRAINT "uni_Person_ResidentIdentityNumber" UNIQUE ("ResidentIdentityNumber"),
  CONSTRAINT "fk_Person_Ethnic" FOREIGN KEY ("Ethnic_Code") REFERENCES "public"."Ethnic" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Person_Language" FOREIGN KEY ("Language_Code") REFERENCES "public"."Language" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "User" table
CREATE TABLE "public"."User" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Name" character varying(25) NOT NULL,
  "Password" character varying(255) NOT NULL,
  "Status_Code" character varying(10) NOT NULL,
  "FailedLoginCount" smallint NULL,
  "Position_Code" character varying(20) NOT NULL,
  "LoginAttemptCount" bigint NULL,
  "LastSuccessLogin" timestamptz NULL,
  "LastAllowdLogin" timestamptz NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_User_Name" UNIQUE ("Name")
);
-- Create "Employee" table
CREATE TABLE "public"."Employee" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "User_Id" bigint NULL,
  "Person_Id" bigint NULL,
  "Division_Code" character varying(10) NULL,
  "Number" character varying(20) NULL,
  "Status_Code" character varying(10) NOT NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Employee_Division" FOREIGN KEY ("Division_Code") REFERENCES "public"."Division" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Employee_Person" FOREIGN KEY ("Person_Id") REFERENCES "public"."Person" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Employee_User" FOREIGN KEY ("User_Id") REFERENCES "public"."User" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Installation" table
CREATE TABLE "public"."Installation" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  "EncounterClass_Code" character varying(10) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Installation_Code" UNIQUE ("Code")
);
-- Create "Unit" table
CREATE TABLE "public"."Unit" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Installation_Id" integer NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  "Type_Code" text NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Unit_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Unit_Installation" FOREIGN KEY ("Installation_Id") REFERENCES "public"."Installation" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Specialist" table
CREATE TABLE "public"."Specialist" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  "Unit_Id" integer NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Specialist_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Specialist_Unit" FOREIGN KEY ("Unit_Id") REFERENCES "public"."Unit" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Subspecialist" table
CREATE TABLE "public"."Subspecialist" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  "Specialist_Id" integer NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Subspecialist_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Subspecialist_Specialist" FOREIGN KEY ("Specialist_Id") REFERENCES "public"."Specialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Doctor" table
CREATE TABLE "public"."Doctor" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Employee_Id" bigint NULL,
  "IHS_Number" character varying(20) NULL,
  "SIP_Number" character varying(20) NULL,
  "Unit_Id" integer NULL,
  "Specialist_Id" integer NULL,
  "Subspecialist_Id" integer NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Doctor_Employee" FOREIGN KEY ("Employee_Id") REFERENCES "public"."Employee" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Doctor_Specialist" FOREIGN KEY ("Specialist_Id") REFERENCES "public"."Specialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Doctor_Subspecialist" FOREIGN KEY ("Subspecialist_Id") REFERENCES "public"."Subspecialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Doctor_Unit" FOREIGN KEY ("Unit_Id") REFERENCES "public"."Unit" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "DoctorFee" table
CREATE TABLE "public"."DoctorFee" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Doctor_Id" bigint NULL,
  "FeeType_Code" character varying(11) NULL,
  "Price" numeric NULL,
  "Item_Id" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_DoctorFee_Doctor" FOREIGN KEY ("Doctor_Id") REFERENCES "public"."Doctor" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_DoctorFee_Item" FOREIGN KEY ("Item_Id") REFERENCES "public"."Item" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Patient" table
CREATE TABLE "public"."Patient" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Person_Id" bigint NULL,
  "RegisteredAt" timestamptz NULL,
  "Status_Code" character varying(10) NOT NULL,
  "Number" character varying(15) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Patient_Number" UNIQUE ("Number"),
  CONSTRAINT "fk_Patient_Person" FOREIGN KEY ("Person_Id") REFERENCES "public"."Person" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Encounter" table
CREATE TABLE "public"."Encounter" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Patient_Id" bigint NULL,
  "RegisteredAt" timestamptz NULL,
  "Class_Code" character varying(10) NOT NULL,
  "Unit_Id" bigint NULL,
  "Specialist_Id" integer NULL,
  "Subspecialist_Id" integer NULL,
  "VisitDate" timestamptz NULL,
  "Assignment_Doctor_Id" bigint NULL,
  "Responsible_Doctor_Id" bigint NULL,
  "DischardeMethod_Code" character varying(10) NULL,
  "RefSource_Name" character varying(100) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Encounter_Assignment_Doctor" FOREIGN KEY ("Assignment_Doctor_Id") REFERENCES "public"."Doctor" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Encounter_Patient" FOREIGN KEY ("Patient_Id") REFERENCES "public"."Patient" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Encounter_Responsible_Doctor" FOREIGN KEY ("Responsible_Doctor_Id") REFERENCES "public"."Doctor" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Encounter_Specialist" FOREIGN KEY ("Specialist_Id") REFERENCES "public"."Specialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Encounter_Subspecialist" FOREIGN KEY ("Subspecialist_Id") REFERENCES "public"."Subspecialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Encounter_Unit" FOREIGN KEY ("Unit_Id") REFERENCES "public"."Unit" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "InsuranceCompany" table
CREATE TABLE "public"."InsuranceCompany" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(20) NULL,
  "Name" character varying(50) NULL,
  "Regency_Code" character varying(4) NULL,
  "Address" character varying(100) NULL,
  "PhoneNumber" character varying(20) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_InsuranceCompany_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_InsuranceCompany_Regency" FOREIGN KEY ("Regency_Code") REFERENCES "public"."Regency" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "ItemPrice" table
CREATE TABLE "public"."ItemPrice" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Item_Id" bigint NULL,
  "Price" numeric NULL,
  "InsuranceCompany_Code" character varying(20) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_ItemPrice_InsuranceCompany" FOREIGN KEY ("InsuranceCompany_Code") REFERENCES "public"."InsuranceCompany" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_ItemPrice_Item" FOREIGN KEY ("Item_Id") REFERENCES "public"."Item" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Laborant" table
CREATE TABLE "public"."Laborant" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Employee_Id" bigint NULL,
  "IHS_Number" character varying(20) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Laborant_Employee" FOREIGN KEY ("Employee_Id") REFERENCES "public"."Employee" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Material" table
CREATE TABLE "public"."Material" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  "Uom_Code" character varying(10) NULL,
  "Infra_Id" integer NULL,
  "Stock" bigint NULL,
  "Item_Id" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Material_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Material_Infra" FOREIGN KEY ("Infra_Id") REFERENCES "public"."Infra" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Material_Item" FOREIGN KEY ("Item_Id") REFERENCES "public"."Item" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Material_Uom" FOREIGN KEY ("Uom_Code") REFERENCES "public"."Uom" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "McuSrcCategory" table
CREATE TABLE "public"."McuSrcCategory" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(20) NULL,
  "Name" character varying(50) NULL,
  "Scope_Code" character varying(10) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_McuSrcCategory_Code" UNIQUE ("Code")
);
-- Create "McuSrc" table
CREATE TABLE "public"."McuSrc" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(20) NULL,
  "Name" character varying(50) NULL,
  "CheckupCategory_Code" character varying(20) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_McuSrc_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_McuSrc_CheckupCategory" FOREIGN KEY ("CheckupCategory_Code") REFERENCES "public"."McuSrcCategory" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "MedicalActionSrc" table
CREATE TABLE "public"."MedicalActionSrc" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(20) NULL,
  "Name" character varying(50) NULL,
  "Item_Id" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_MedicalActionSrc_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_MedicalActionSrc_Item" FOREIGN KEY ("Item_Id") REFERENCES "public"."Item" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "ProcedureSrc" table
CREATE TABLE "public"."ProcedureSrc" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(2048) NULL,
  "IndName" character varying(2048) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_ProcedureSrc_Code" UNIQUE ("Code")
);
-- Create "MedicalActionSrcItem" table
CREATE TABLE "public"."MedicalActionSrcItem" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "MedicalActionSrc_Id" bigint NULL,
  "ProcedureSrc_Id" bigint NULL,
  "Item_Id" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_MedicalActionSrcItem_Item" FOREIGN KEY ("Item_Id") REFERENCES "public"."Item" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_MedicalActionSrcItem_MedicalActionSrc" FOREIGN KEY ("MedicalActionSrc_Id") REFERENCES "public"."MedicalActionSrc" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_MedicalActionSrcItem_ProcedureSrc" FOREIGN KEY ("ProcedureSrc_Id") REFERENCES "public"."ProcedureSrc" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "MedicineGroup" table
CREATE TABLE "public"."MedicineGroup" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_MedicineGroup_Code" UNIQUE ("Code")
);
-- Create "MedicineMethod" table
CREATE TABLE "public"."MedicineMethod" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_MedicineMethod_Code" UNIQUE ("Code")
);
-- Create "Medicine" table
CREATE TABLE "public"."Medicine" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  "MedicineGroup_Code" character varying(10) NULL,
  "MedicineMethod_Code" character varying(10) NULL,
  "Uom_Code" character varying(10) NULL,
  "Dose" smallint NULL,
  "Infra_Id" integer NULL,
  "Stock" bigint NULL,
  "Item_Id" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Medicine_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_Medicine_Infra" FOREIGN KEY ("Infra_Id") REFERENCES "public"."Infra" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Medicine_Item" FOREIGN KEY ("Item_Id") REFERENCES "public"."Item" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Medicine_MedicineGroup" FOREIGN KEY ("MedicineGroup_Code") REFERENCES "public"."MedicineGroup" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Medicine_MedicineMethod" FOREIGN KEY ("MedicineMethod_Code") REFERENCES "public"."MedicineMethod" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Medicine_Uom" FOREIGN KEY ("Uom_Code") REFERENCES "public"."Uom" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "MedicineMix" table
CREATE TABLE "public"."MedicineMix" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id")
);
-- Create "MedicineMixItem" table
CREATE TABLE "public"."MedicineMixItem" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "MedicineMix_Id" bigint NULL,
  "Medicine_Id" bigint NULL,
  "Dose" smallint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_MedicineMixItem_Medicine" FOREIGN KEY ("Medicine_Id") REFERENCES "public"."Medicine" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_MedicineMixItem_MedicineMix" FOREIGN KEY ("MedicineMix_Id") REFERENCES "public"."MedicineMix" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Nurse" table
CREATE TABLE "public"."Nurse" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Employee_Id" bigint NULL,
  "IHS_Number" character varying(20) NULL,
  "Unit_Id" integer NULL,
  "Infra_Id" integer NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Nurse_Employee" FOREIGN KEY ("Employee_Id") REFERENCES "public"."Employee" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Nurse_Infra" FOREIGN KEY ("Infra_Id") REFERENCES "public"."Infra" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Nurse_Unit" FOREIGN KEY ("Unit_Id") REFERENCES "public"."Unit" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Nutritionist" table
CREATE TABLE "public"."Nutritionist" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Employee_Id" bigint NULL,
  "IHS_Number" character varying(20) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Nutritionist_Employee" FOREIGN KEY ("Employee_Id") REFERENCES "public"."Employee" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "PersonAddress" table
CREATE TABLE "public"."PersonAddress" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Person_Id" bigint NULL,
  "Address" character varying(150) NULL,
  "Rt" character varying(2) NULL,
  "Rw" character varying(2) NULL,
  "Village_Code" character varying(10) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Person_Addresses" FOREIGN KEY ("Person_Id") REFERENCES "public"."Person" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "PersonContact" table
CREATE TABLE "public"."PersonContact" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Person_Id" bigint NULL,
  "Type_Code" character varying(15) NULL,
  "Value" character varying(100) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Person_Contacts" FOREIGN KEY ("Person_Id") REFERENCES "public"."Person" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Village" table
CREATE TABLE "public"."Village" (
  "Id" bigserial NOT NULL,
  "District_Code" character varying(6) NULL,
  "Code" character varying(10) NULL,
  "Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_Village_Code" UNIQUE ("Code"),
  CONSTRAINT "fk_District_Villages" FOREIGN KEY ("District_Code") REFERENCES "public"."District" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "PersonRelative" table
CREATE TABLE "public"."PersonRelative" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Person_Id" bigint NULL,
  "Relationship_Code" character varying(100) NOT NULL,
  "Name" character varying(100) NULL,
  "Address" character varying(100) NULL,
  "Village_Code" character varying(10) NULL,
  "Gender_Code" character varying(10) NULL,
  "PhoneNumber" character varying(30) NULL,
  "Education_Code" character varying(10) NULL,
  "Occupation_Code" character varying(10) NULL,
  "Occupation_Name" character varying(50) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_PersonRelative_Village" FOREIGN KEY ("Village_Code") REFERENCES "public"."Village" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Person_Relatives" FOREIGN KEY ("Person_Id") REFERENCES "public"."Person" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Pharmacist" table
CREATE TABLE "public"."Pharmacist" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Employee_Id" bigint NULL,
  "IHS_Number" character varying(20) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Pharmacist_Employee" FOREIGN KEY ("Employee_Id") REFERENCES "public"."Employee" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "PracticeSchedule" table
CREATE TABLE "public"."PracticeSchedule" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Doctor_Id" bigint NULL,
  "Unit_Code" character varying(10) NULL,
  "Day_Code" smallint NULL,
  "StartTime" character varying(5) NULL,
  "EndTime" character varying(5) NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_PracticeSchedule_Doctor" FOREIGN KEY ("Doctor_Id") REFERENCES "public"."Doctor" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_PracticeSchedule_Unit" FOREIGN KEY ("Unit_Code") REFERENCES "public"."Unit" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "Room" table
CREATE TABLE "public"."Room" (
  "Id" serial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Infra_Id" integer NULL,
  "Unit_Id" integer NULL,
  "Specialist_Id" integer NULL,
  "Subspecialist_Id" integer NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_Room_Infra" FOREIGN KEY ("Infra_Id") REFERENCES "public"."Infra" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Room_Specialist" FOREIGN KEY ("Specialist_Id") REFERENCES "public"."Specialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Room_Subspecialist" FOREIGN KEY ("Subspecialist_Id") REFERENCES "public"."Subspecialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_Room_Unit" FOREIGN KEY ("Unit_Id") REFERENCES "public"."Unit" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "SpecialistIntern" table
CREATE TABLE "public"."SpecialistIntern" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Person_Id" bigint NULL,
  "Specialist_Id" integer NULL,
  "Subspecialist_Id" integer NULL,
  "User_Id" bigint NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_SpecialistIntern_Person" FOREIGN KEY ("Person_Id") REFERENCES "public"."Person" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_SpecialistIntern_Specialist" FOREIGN KEY ("Specialist_Id") REFERENCES "public"."Specialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_SpecialistIntern_Subspecialist" FOREIGN KEY ("Subspecialist_Id") REFERENCES "public"."Subspecialist" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_SpecialistIntern_User" FOREIGN KEY ("User_Id") REFERENCES "public"."User" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
