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

module.exports = (sequelize) => {
	const Medicine = sequelize.define(
		"medicine",
		{
			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 },

			// main
			name: { type: DataTypes.STRING(50), allowNull: false },
			display_name: { type: DataTypes.STRING(50), allowNull: false },
			generic_name: { type: DataTypes.STRING(100), allowNull: true },
			description: { type: DataTypes.TEXT, allowNull: true },
			medicine_image: { type: DataTypes.STRING, allowNull: true },
			strength: { type: DataTypes.FLOAT, allowNull: true },

			composition: { type: DataTypes.TEXT, allowNull: true },
			benefits: { type: DataTypes.TEXT, allowNull: true },
			side_effects: { type: DataTypes.TEXT, allowNull: true },
			general_indication: { type: DataTypes.TEXT, allowNull: true },
			contraindication: { type: DataTypes.TEXT, allowNull: true },
			attention: { type: DataTypes.TEXT, allowNull: true },

			price: { type: DataTypes.INTEGER, allowNull: true },
			registration_number: { type: DataTypes.STRING(150), allowNull: true },

			active_ingredient: { type: DataTypes.TEXT, allowNull: true },

			// fk
			dosageform_id: { type: DataTypes.INTEGER, allowNull: true },
			dosageunit_id: { type: DataTypes.INTEGER, allowNull: true },
			manufacture_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "medicines",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the Medicine model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	Medicine.associate = (models) => {
		Medicine.belongsTo(models.DosageForm, { foreignKey: "dosageform_id", as: "dosageform" });
		Medicine.belongsTo(models.DosageUnit, { foreignKey: "dosageunit_id", as: "dosageunit" });
		Medicine.belongsTo(models.Manufacturer, { foreignKey: "manufacture_id", as: "manufacture" });
	};

	return Medicine;
};
