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

module.exports = (sequelize) => {
	const Gender = sequelize.define(
		"gender",
		{
			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
			name: { type: DataTypes.STRING(50), allowNull: false },
			display_name: { type: DataTypes.STRING(50), allowNull: false },

			// fk
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
			},
		},
		{
			tableName: "genders",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the Gender model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	Gender.associate = (models) => {
		Gender.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

		Gender.hasMany(models.Employee, { foreignKey: "gender_id", as: "employees" });

		Gender.hasMany(models.Patient, { foreignKey: "gender_id", as: "patients" });

		Gender.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
	};

	return Gender;
};
