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

module.exports = (sequelize) => {
	const BloodPressureMonitoring = sequelize.define(
		"bloodpressuremonitoring",
		{
			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 }, // User or doctor who owns the record

			// main
			day: { type: DataTypes.STRING(100), allowNull: false }, // Hari ke berapa pengukuran dilakukan
			date: { type: DataTypes.DATEONLY, allowNull: false }, // Tanggal pengukuran tekanan darah
			time: { type: DataTypes.TIME, allowNull: false }, // Waktu pengukuran tekanan darah
			systolic: { type: DataTypes.INTEGER, allowNull: false }, // Tekanan darah sistolik
			diastolic: { type: DataTypes.INTEGER, allowNull: false }, // Tekanan darah diastolik
			pulse: { type: DataTypes.INTEGER, allowNull: true }, // Denyut nadi
			take_medicine: { type: DataTypes.BOOLEAN, allowNull: true }, // Apakah sudah minum obat atau belum

			notes: { type: DataTypes.TEXT, allowNull: true }, // Catatan tambahan

			patient_id: { type: DataTypes.INTEGER, allowNull: false }, // FK ke pasien/User
			prescription_id: { type: DataTypes.INTEGER, allowNull: true }, // FK ke resep jika ada
		},
		{
			tableName: "bloodpressuremonitorings",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the Prescription model with other models.
	 * @param {object} models - The models to associate with.
	 */
	BloodPressureMonitoring.associate = (models) => {
		BloodPressureMonitoring.belongsTo(models.Patient, { foreignKey: "patient_id", as: "patient" });
		BloodPressureMonitoring.belongsTo(models.Prescription, { foreignKey: "prescription_id", as: "prescription" });
		BloodPressureMonitoring.hasMany(models.BloodPressureMonitoringMedicine, {
			foreignKey: "bloodpressuremonitoring_id",
			as: "bloodpressuremonitoringmedicines",
		});
	};

	return BloodPressureMonitoring;
};
