const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
	const Patient = sequelize.define(
		"patient",
		{
			id: {
				type: DataTypes.INTEGER,
				autoIncrement: true,
				primaryKey: true,
			},
			creation: { type: DataTypes.DATE, allowNull: false },
			modified: { type: DataTypes.DATE, allowNull: false },
			modified_by: { type: DataTypes.INTEGER, allowNull: false },
			owner: { type: DataTypes.INTEGER, allowNull: false },
			enabled: { type: DataTypes.BOOLEAN, allowNull: false },

			// main
			patient_no: { type: DataTypes.STRING(50), allowNull: true },
			full_name: { type: DataTypes.STRING(255), allowNull: false },
			email: { type: DataTypes.STRING(255), allowNull: true },
			date_of_birth: { type: DataTypes.DATEONLY, allowNull: true },
			age: { type: DataTypes.INTEGER, allowNull: true },
			address: { type: DataTypes.STRING(150), allowNull: true },
			mobile_no: { type: DataTypes.STRING(50), allowNull: true },
			patient_image: { type: DataTypes.STRING, allowNull: true },
			phone_number:  { type: DataTypes.STRING(50), allowNull: true },

			// fk
			user_id: { type: DataTypes.INTEGER, allowNull: true },
			hospital_id: { type: DataTypes.INTEGER, allowNull: true },
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			religion_id: { type: DataTypes.INTEGER, allowNull: true },
			bloodgroup_id: { type: DataTypes.INTEGER, allowNull: true },
			maritalstatus_id: { type: DataTypes.INTEGER, allowNull: true },
			occupation_id: { type: DataTypes.INTEGER, allowNull: true },
			nationality_id: { type: DataTypes.INTEGER, allowNull: true },
			gender_id: { type: DataTypes.INTEGER, allowNull: true },
			age_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "patients",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the Patient model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	Patient.associate = (models) => {
		Patient.belongsTo(models.User, { foreignKey: "user_id", as: "user" });
		Patient.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		Patient.belongsTo(models.Religion, { foreignKey: "religion_id", as: "religion" });
		Patient.belongsTo(models.BloodGroup, { foreignKey: "bloodgroup_id", as: "bloodgroup" });
		Patient.belongsTo(models.MaritalStatus, { foreignKey: "maritalstatus_id", as: "maritalstatus" });
		Patient.belongsTo(models.Occupation, { foreignKey: "occupation_id", as: "occupation" });
		Patient.belongsTo(models.Nationality, { foreignKey: "nationality_id", as: "nationality" });
		Patient.belongsTo(models.Gender, { foreignKey: "gender_id", as: "gender" });
		Patient.belongsTo(models.Age, { foreignKey: "age_id", as: "agerange" });
		Patient.belongsTo(models.Hospital, { foreignKey: "hospital_id", as: "hospital" });
		Patient.hasMany(models.Prescription, { foreignKey: "patient_id", as: "prescriptions" });
		Patient.hasMany(models.BloodPressureMonitoring, { foreignKey: "patient_id", as: "bloodPressureMonitorings" });
	};

	return Patient;
};
