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

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

			// fk
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
				references: {
					model: "companies",
					key: "id",
				},
			},
		},
		{
			tableName: "jobroles",
			freezeTableName: true,
		},
	);

	JobRole.associate = (models) => {
		JobRole.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		JobRole.hasMany(models.Employee, { foreignKey: "jobrole_id", as: "employees" });
	};

	return JobRole;
};
