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

module.exports = (sequelize) => {
	const EducationLevel = sequelize.define(
		"educationlevel",
		{
			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, defaultValue: true },

			// default timestamps (system pattern)
			createdAt: { type: DataTypes.DATE, allowNull: true, field: "createdAt" },
			updatedAt: { type: DataTypes.DATE, allowNull: true, field: "updatedAt" },

			// main
			name: { type: DataTypes.STRING(50), allowNull: false },
			display_name: { type: DataTypes.STRING(100), allowNull: false },
			level_order: { type: DataTypes.INTEGER, allowNull: true },
			description: { type: DataTypes.TEXT, allowNull: true },

			// fk
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
			},
		},
		{
			tableName: "educationlevels",
			freezeTableName: true,
			indexes: [
				{ name: "idx_educationlevels_company_id", fields: ["company_id"] },
				{ name: "idx_educationlevels_enabled", fields: ["enabled"] },
				{ name: "idx_educationlevels_level_order", fields: ["level_order"] },
			],
		}
	);

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

		EducationLevel.hasMany(models.Employee, {
			foreignKey: "education_level_id",
			as: "employees",
		});
	};

	return EducationLevel;
};
