import config from "config";
import { DataSource } from "typeorm";
import { ILogObj, Logger } from "tslog";
import {
	ResponsiblePartyConsent,
	Province,
	City,
	Subdistrict,
	Ward,
	RefferalHospital,
	RegistrationFee,
	FareType,
	RoomStock,
	Administrativu,
	Aldeia,
	District,
	Menu,
	Munisipo,
	Suco,
	Application,
	Nationality,
	Diagnosis,
	DiagnosticProcedure,
	Room,
	MedicalForm,
	ServiceType,
	ServiceClass,
	Service,
	ServiceFare,
	ServicePackage,
	Department,
	DoctorSchedule,
	MeasurementUnit,
	ReferenceRange,
	QueueMonitoring,
	QueueMonitoringRooms,
	ExaminationDetail,
	ExaminationType,
	User,
	UserRole,
	HrmsEmployee,
	HrmsDepartment,
	HrmsPosition,
	HrmsShift,
	HospitalInformation,
	ItemOrigin,
	ItemType,
	ItemTypeDetail,
	Unit,
	ItemCategory,
	ItemStatus,
	UsageInstructions,
	UsageTime,
	ItemClass,
	ItemClassDetail,
	GenericName,
	Factory,
	Supplier,
	FactoryToSupplier,
	ItemMaster,
	ItemGroup,
	PharmacyInfo,
	ItemPrice,
	SellingPricePercentage,
	PatientGroup,
	Currency,
	InpatientRoom,
	SurgeryRoom,
	SurgeryType,
	PatientGuarantor,
	InitialStock,
	DoctorItem,
	DoctorItemPackage,
	UserToRoom,
	RoomToPharmacy,
	PlanningVerificator,
	ScheduleSeries,
	ScheduleException,
	CardType,
	PaymentMethod,
	FosterCareHistory,
	InpatientRoomBed,
	EmrBoardingRoom,
	EmrPatient,
	EmrRegistration,
	EmrDiagnosis,
	EmrRegistrationOutpatient,
	EmrRegistrationEmergency,
	EmrRegistrationEmergencyResponsible,
	DischargeStatusEmergency,
	EmrInternalConsultation,
	Counter,
	RoomNumber,
	MainDisease,
	CardKeySet,
	EmrRegistrationTreatment,
	TreatmentPlanSession,
	TreatmentPlan
} from "entity";

export class OrmHelper {
	static DB: DataSource = null;

	static setup() {
		const log: Logger<ILogObj> = new Logger({
			name: "[OrmHelper]",
			type: "pretty",
		});

		const engine: "mysql" | "postgres" = config.get("database.engine");

		OrmHelper.DB = new DataSource({
			type: engine,
			host: config.get("database.host"),
			port: Number(config.get("database.port")),
			username: String(config.get("database.username")),
			password: String(config.get("database.password")),
			database: String(config.get("database.database")),
			synchronize: true,
			logging: config.get("database.logging"),
			entities: [
				//
				Menu,
				Administrativu,
				Aldeia,
				District,
				Munisipo,
				Suco,
				Application,
				Nationality,
				Diagnosis,
				DiagnosticProcedure,
				Room,
				MedicalForm,
				ServiceType,
				ServiceClass,
				Service,
				ServiceFare,
				ServicePackage,
				Department,
				DoctorSchedule,
				MeasurementUnit,
				ReferenceRange,
				QueueMonitoring,
				QueueMonitoringRooms,
				ExaminationDetail,
				ExaminationType,
				User,
				UserRole,
				HrmsEmployee,
				HrmsDepartment,
				HrmsPosition,
				HospitalInformation,
				HrmsShift,
				ItemGroup,
				ItemOrigin,
				ItemType,
				ItemTypeDetail,
				Unit,
				Currency,
				InpatientRoom,
				InpatientRoomBed,
				EmrPatient,
				EmrRegistration,
				EmrDiagnosis,
				EmrRegistrationOutpatient,
				EmrRegistrationEmergency,
				EmrRegistrationEmergencyResponsible,
				DischargeStatusEmergency,
				EmrInternalConsultation,
				EmrBoardingRoom,
				SurgeryRoom,
				SurgeryType,
				ItemCategory,
				ItemStatus,
				UsageInstructions,
				UsageTime,
				ItemClass,
				ItemClassDetail,
				GenericName,
				Factory,
				Supplier,
				FactoryToSupplier,
				ItemMaster,
				PharmacyInfo,
				ItemPrice,
				SellingPricePercentage,
				PatientGroup,
				PatientGuarantor,
				InitialStock,
				DoctorItem,
				DoctorItemPackage,
				UserToRoom,
				RoomToPharmacy,
				PlanningVerificator,
				RoomStock,
				ScheduleSeries,
				ScheduleException,
				FareType,
				CardType,
				PaymentMethod,
				RegistrationFee,
				RefferalHospital,
				Province,
				City,
				Subdistrict,
				Ward,
				ResponsiblePartyConsent,
				FosterCareHistory,
				Counter,
				RoomNumber,
				MainDisease,
				CardKeySet,
				EmrRegistrationTreatment,
				TreatmentPlanSession,
				TreatmentPlan
			],
			subscribers: [],
			migrations: [],
		});

		OrmHelper.DB.initialize()
			.then(() => {
				// here you can start to work with your database
			})
			.catch((error: any) => log.error(error));
	}
}
