alter table public."PharmacyProduct" drop column if exists "MaxQuantity",
drop column if exists  "MinQuantity",drop column if exists  "RolQuantity",
drop column if exists "RackId",drop column if exists "Pharmacy_Source";


alter table public."PharmacyProduct" add column if not exists "Potency" text, 
add column if not exists "DrugRiskId" int references "LookupValue"("LookupValueId"),
add column if not exists "Dosage" text,add column if not exists "Formula" text,
add column if not exists "NABHCategoryId" int references "LookupValue"("LookupValueId"),
add column if not exists "DosageTypeId" int references "LookupValue"("LookupValueId"),
add column if not exists "Strength" text,add column if not exists "FixedDose" numeric,
add column if not exists  "MedFrequencyMasterId" int references public."MedFrequencyMaster"("MedFrequencyMasterId"),
add column if not exists "NoOfTimes" int,add column if not exists "SaleLoose" boolean,
add column if not exists "InventoryExpiry" int,add column if not exists "FormulationId" int references "LookupValue"("LookupValueId"),
add column if not exists "MedRouteId" int references "LookupValue"("LookupValueId");


insert into "Lookup" ("Name","Description")
values
('PharmacyDrugRisk', 'For pharmacy drug risks');

insert into "LookupValue" ("LookupId","Name","CreatedBy", "CreatedDate")
values 
((select "LookupId" from "Lookup" where "Name"= 'PharmacyDrugRisk'),'Low Risk',1029, now()),
((select "LookupId" from "Lookup" where "Name"= 'PharmacyDrugRisk'),'Medium Risk',1029, now()),
((select "LookupId" from "Lookup" where "Name"= 'PharmacyDrugRisk'),'High Risk',1029, now());


insert into "Lookup" ("Name","Description")
values
('NABHCategory', 'For NABH Authority');

insert into "LookupValue" ("LookupId","Name","CreatedBy", "CreatedDate")
values 
((select "LookupId" from "Lookup" where "Name"= 'NABHCategory'),'Vital',1029, now()),
((select "LookupId" from "Lookup" where "Name"= 'NABHCategory'),'Essential',1029, now()),
((select "LookupId" from "Lookup" where "Name"= 'NABHCategory'),'Desirable',1029, now());


insert into "Lookup" ("Name","Description")
values
('PharmacyDosageType', 'To set dose for pharmacy product');

insert into "LookupValue" ("LookupId","Name","CreatedBy", "CreatedDate")
values 
((select "LookupId" from "Lookup" where "Name"= 'PharmacyDosageType'),'Regular Items',1029, now()),
((select "LookupId" from "Lookup" where "Name"= 'PharmacyDosageType'),'Weight Wise',1029, now()),
((select "LookupId" from "Lookup" where "Name"= 'PharmacyDosageType'),'Age Wise',1029, now());


insert into "Lookup" ("Name","Description")
values
('PharmacyFormulation', 'To set formulation for pharmacy product');


alter table public."PharmacyProduct" add column if not exists "AlchoholInteraction" text,
 add column if not exists "PregnancyInteraction" text,
add column if not exists "ExpertAdvice" text,
add column if not exists "CommonSideEffects" text,
add column if not exists "MedicineFaq" text,
add column if not exists "MedicineInteraction" text,
add column if not exists "Usage" text;

create table public."VaccineAgeGroup"(
					"VaccineAgeGroupId" serial primary key,
					"AgeGroupName" text	
);

insert into public."VaccineAgeGroup"( "AgeGroupName")
values
('At Birth'),
('1 to 6 Weeks'),
('7 to 10 Weeks'),
('3 to 4 Months'),
('5 to 6 Months'),
('7 to 8 Months'),
('8 to 9 Months'),
('10 to 11 Months'),
('12 to 13 Months'),
('13 to 15 Months'),
('16 to 17 Months'),
('17 to 18 Months'),
('2 to 4 Years'),
('4 to 6 Years'),
('6 to 9 Years'),
('10 to 12 Years'),
('12 to 18 Years'),
('18 to 100 Years');

alter table public."VaccineMaster" add column if not exists "VaccineAgeGroupId" int references "VaccineAgeGroup"("VaccineAgeGroupId");

drop table "VaccineMasterPharmacyLink" ;

create table public."VaccinePharmacyLinkHeader"(
			"VaccinePharmacyLinkHeaderId" serial primary key,
			"PharmacyProductId" int references "PharmacyProduct"("PharmacyProductId"),
			"CreatedBy" int references "Account"("AccountId"),
			"CreatedDate" timestamp without time zone,
			"ModifiedBy" int references "Account"("AccountId"),
			"ModifiedDate" timestamp without time zone
);

create table public."VaccinePharmacyLinkDetail"(
					"VaccinePharmacyLinkDetailId" serial primary key,
					"VaccinePharmacyLinkHeaderId" int references "VaccinePharmacyLinkHeader"("VaccinePharmacyLinkHeaderId"),
					"VaccineMasterId" int references "VaccineMaster"("VaccineMasterId")
);

create table public."ImmunizationHistory"(
					"ImmunizationHistoryId" serial primary key,
					"PharmacyProductId" int references "PharmacyProduct"("PharmacyProductId"),
					"VaccineMasterId" int references "VaccineMaster"("VaccineMasterId"),
					"ProductName" text,
					"BillNumber" varchar(300),
					"BatchNumber" varchar(300),
					"ExpiryDate" date,
					"CreatedBy" int references "Account"("AccountId"),
					"CreatedDate" timestamp without time zone
);

alter table public."ImmunizationHistory" add column if not exists
		"Status" varchar(50);

alter table public."ImmunizationHistory" add column if not exists
		"PatientId" int references "Patient"("PatientId");

alter table "ImmunizationHistory"
	add column if not exists "VaccineGivenBy" int references "Account"("AccountId"),
	add column if not exists "VaccineGivenDate" timestamp without time zone;



alter table "PediatricEncounter" add column if not exists "VaccineDetail" text;

UPDATE public."Menu"
SET  "EncounterKey"='vaccineDetail'
where "Url"='/app/pediatric-encounter/:id/:type/immunization';

alter table "PediatricAgeWiseData"
add column if not exists "M" numeric, 
add column if not exists "L" numeric, 
add column if not exists "P999" numeric,
 add column if not exists "P01" numeric,
 add column if not exists "P10" numeric,
 add column if not exists "P90" numeric,
add column if not exists "S" numeric,
add column if not exists "Minus3SD" numeric,
add column if not exists "Minus2SD" numeric,
add column if not exists "Minus1SD" numeric,
add column if not exists "Median" numeric,
add column if not exists "1SD" numeric,
add column if not exists "2SD" numeric,
add column if not exists "3SD" numeric; 


create table vendors."SupplierProductHeader"(
						"SupplierProductHeaderId" serial primary key,
						"SupplierId" int references "Supplier"("SupplierId"),
						"StartDate" timestamp without time zone,
						"EndDate" timestamp without time zone,
						"CreatedBy" int references "Account"("AccountId"),
						"CreatedDate" timestamp without time zone,
						"ModifiedBy" int references "Account"("AccountId"),
						"ModifiedDate" timestamp without time zone
);

create table vendors."SupplierProductFormulation"(
				"SupplierProductFormulationId" serial primary key,
				"SupplierProductHeaderId" int references vendors."SupplierProductHeader"("SupplierProductHeaderId"), 
				"Operator" varchar(50),
				"Condition" varchar(50),
				"FOCQuantity" int 
);


CREATE TABLE if not exists vendors."SupplierProductDetail" (
	"SupplierProductDetailId" serial primary key,
	"SupplierProductHeaderId" int ,
	"PharmacyProductId" int ,
	"PurchaseRate" numeric ,
	"Mrp" numeric 
);

ALTER TABLE vendors."SupplierProductDetail" ADD CONSTRAINT if not exists "SupplierProductDetail_PharmacyProductId_fkey" FOREIGN KEY ("PharmacyProductId") REFERENCES public."PharmacyProduct"("PharmacyProductId");
ALTER TABLE vendors."SupplierProductDetail" ADD CONSTRAINT if not exists "SupplierProductDetail_SupplierProductHeaderId_fkey" FOREIGN KEY ("SupplierProductHeaderId") REFERENCES vendors."SupplierProductHeader"("SupplierProductHeaderId");

alter table vendors."PharmacyProductApproval" add column if not exists "Free" int;


alter table vendors."PurchaseOrderDetail" add column if not exists "Free" int;

alter table public."PharmacyProduct" add column "InventoryItem" boolean default false;

create table public."PharmacyInPatientReturn"(
			"PharmacyInPatientReturnId" bigserial primary key,
			"PharmacyIssueDetailId" int references "PharmacyIssueDetail"("PharmacyIssueDetailId"),
			"ReturnQuantity" int,
			"CreatedBy" int references "Account"("AccountId"),
			"CreatedDate" timestamp without time zone,
			"AcceptedBy" int references "Account"("AccountId"),
			"AcceptedDate" timestamp without time zone
);


alter table "PharmacyIssueDetail" add column "PharmacyIndentDetailId" int references "PharmacyIndentDetail"("PharmacyIndentDetailId");