-- 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 "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,
  "LoginAttemptCount" bigint NULL,
  "LastSuccessLogin" timestamptz NULL,
  "LastAllowdLogin" timestamptz NULL,
  PRIMARY KEY ("Id")
);
-- 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 "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,
  "BirthDate" timestamptz NULL,
  "BirthRegency_Code" character varying(4) NULL,
  "Gender_Code" character varying(10) NULL,
  "ResidentIdentityNumber" character varying(16) 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(15) NULL,
  PRIMARY KEY ("Id")
);
-- 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(10) 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 "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,
  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 "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
);
