-- Modify "ApMcuOrder" table
ALTER TABLE "public"."ApMcuOrder" DROP COLUMN "McuOrder_Id", ADD COLUMN "Encounter_Id" bigint NULL, ADD COLUMN "Number" bigint NULL, ADD COLUMN "Doctor_Code" character varying(20) NULL, ADD CONSTRAINT "uni_ApMcuOrder_Doctor_Code" UNIQUE ("Doctor_Code"), ADD CONSTRAINT "fk_ApMcuOrder_Doctor" FOREIGN KEY ("Doctor_Code") REFERENCES "public"."Doctor" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION, ADD CONSTRAINT "fk_ApMcuOrder_Encounter" FOREIGN KEY ("Encounter_Id") REFERENCES "public"."Encounter" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION;
-- Create "CpMcuOrder" table
CREATE TABLE "public"."CpMcuOrder" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Encounter_Id" bigint NULL,
  "Number" bigint NULL,
  "Doctor_Code" character varying(20) NULL,
  "UrgencyLevel_Code" character varying(15) NOT NULL,
  "OtherNotes" text NULL,
  "ExamScheduleDate" timestamptz NULL,
  "Resume" text NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_CpMcuOrder_Doctor_Code" UNIQUE ("Doctor_Code"),
  CONSTRAINT "fk_CpMcuOrder_Doctor" FOREIGN KEY ("Doctor_Code") REFERENCES "public"."Doctor" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_CpMcuOrder_Encounter" FOREIGN KEY ("Encounter_Id") REFERENCES "public"."Encounter" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "CpMcuOrderItem" table
CREATE TABLE "public"."CpMcuOrderItem" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "CpMcuOrder_Id" bigint NULL,
  "McuSrc_Code" character varying(20) NULL,
  "Note" character varying(1024) NULL,
  "Result" text NULL,
  "Status_Code" text NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_CpMcuOrderItem_CpMcuOrder" FOREIGN KEY ("CpMcuOrder_Id") REFERENCES "public"."CpMcuOrder" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_CpMcuOrderItem_McuSrc" FOREIGN KEY ("McuSrc_Code") REFERENCES "public"."McuSrc" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "MicroMcuOrder" table
CREATE TABLE "public"."MicroMcuOrder" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Encounter_Id" bigint NULL,
  "Number" bigint NULL,
  "Doctor_Code" character varying(20) NULL,
  "Stage_Code" character varying(10) NOT NULL,
  "AxillaryTemp" numeric NULL,
  "OtherNotes" text NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_MicroMcuOrder_Doctor_Code" UNIQUE ("Doctor_Code"),
  CONSTRAINT "fk_MicroMcuOrder_Doctor" FOREIGN KEY ("Doctor_Code") REFERENCES "public"."Doctor" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_MicroMcuOrder_Encounter" FOREIGN KEY ("Encounter_Id") REFERENCES "public"."Encounter" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "MicroMcuOrderItem" table
CREATE TABLE "public"."MicroMcuOrderItem" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "MicroMcuOrder_Id" bigint NULL,
  "McuSrc_Code" character varying(20) NULL,
  "Note" character varying(1024) NULL,
  "Result" text NULL,
  "Status_Code" text NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_MicroMcuOrderItem_McuSrc" FOREIGN KEY ("McuSrc_Code") REFERENCES "public"."McuSrc" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_MicroMcuOrderItem_MicroMcuOrder" FOREIGN KEY ("MicroMcuOrder_Id") REFERENCES "public"."MicroMcuOrder" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "RadiologyMcuOrder" table
CREATE TABLE "public"."RadiologyMcuOrder" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "Encounter_Id" bigint NULL,
  "Number" bigint NULL,
  "Doctor_Code" character varying(20) NULL,
  "ClinicalNotes" text NULL,
  "OtherNotes" text NULL,
  "Resume" text NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "uni_RadiologyMcuOrder_Doctor_Code" UNIQUE ("Doctor_Code"),
  CONSTRAINT "fk_RadiologyMcuOrder_Doctor" FOREIGN KEY ("Doctor_Code") REFERENCES "public"."Doctor" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_RadiologyMcuOrder_Encounter" FOREIGN KEY ("Encounter_Id") REFERENCES "public"."Encounter" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "RadiologyMcuOrderItem" table
CREATE TABLE "public"."RadiologyMcuOrderItem" (
  "Id" bigserial NOT NULL,
  "CreatedAt" timestamptz NULL,
  "UpdatedAt" timestamptz NULL,
  "DeletedAt" timestamptz NULL,
  "RadiologyMcuOrder_Id" bigint NULL,
  "McuSrc_Code" character varying(20) NULL,
  "Note" character varying(1024) NULL,
  "Result" text NULL,
  "Status_Code" text NULL,
  PRIMARY KEY ("Id"),
  CONSTRAINT "fk_RadiologyMcuOrderItem_McuSrc" FOREIGN KEY ("McuSrc_Code") REFERENCES "public"."McuSrc" ("Code") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "fk_RadiologyMcuOrderItem_RadiologyMcuOrder" FOREIGN KEY ("RadiologyMcuOrder_Id") REFERENCES "public"."RadiologyMcuOrder" ("Id") ON UPDATE NO ACTION ON DELETE NO ACTION
);
