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

module.exports = (sequelize) => {
	const BloodPressureMonitoringMedicine = sequelize.define(
		"bloodpressuremonitoringmedicine",
		{
			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
			
			medicine_taken: { type: DataTypes.BOOLEAN, allowNull: true }, // Nama obat yang diminum
			notes: { type: DataTypes.TEXT, allowNull: true }, // Catatan tambahan
			available_quantity: { type: DataTypes.FLOAT, allowNull: true },
			total_quantity: { type: DataTypes.FLOAT, allowNull: true },

			patient_id: { type: DataTypes.INTEGER, allowNull: true }, // FK ke pasien/User
			medicine_id: { type: DataTypes.INTEGER, allowNull: true }, // FK ke medicine
			prescription_id: { type: DataTypes.INTEGER, allowNull: true }, // FK ke resep jika ada
			prescriptionmedicine_id: { type: DataTypes.INTEGER, allowNull: true },
			bloodpressuremonitoring_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "bloodpressuremonitoringmedicines",
			freezeTableName: true,
		}
	);

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

	return BloodPressureMonitoringMedicine;
};
