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

module.exports = (sequelize) => {
	const PrescriptionMedicine = sequelize.define(
		"prescriptionmedicine",
		{
			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 details in prescription
			dosage: { type: DataTypes.TEXT, allowNull: true },
			dosage_frequency: { type: DataTypes.FLOAT, allowNull: true }, // Berapa kali dosis diberikan dalam 1 hari
			dosage_quantity: { type: DataTypes.FLOAT, allowNull: true }, // Jumlah dosis yang diberikan, misal: "1 tablet"
			dosage_interval: { type: DataTypes.FLOAT, allowNull: true }, // Frekuensi pemberian, misal: "Setiap 8 jam"

			dosage_duration: { type: DataTypes.FLOAT, allowNull: true }, // Lama konsumsi, misal: "7 hari"
			available_quantity: { type: DataTypes.FLOAT, allowNull: true }, // Jumlah obat yang tersedia
			total_quantity: { type: DataTypes.FLOAT, allowNull: true }, // Jumlah yang diberikan dalam resep

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

			medicine_id: { type: DataTypes.INTEGER, allowNull: false }, // FK ke medicine
			prescription_id: { type: DataTypes.INTEGER, allowNull: false },
		},
		{
			tableName: "prescriptionmedicines",
			freezeTableName: true,
		}
	);

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

	return PrescriptionMedicine;
};
