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

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

			from_date: { type: DataTypes.DATEONLY, allowNull: false },
			to_date: { type: DataTypes.DATEONLY, allowNull: false },
			periode_month: { type: DataTypes.STRING, allowNull: false },
			periode_year: { type: DataTypes.STRING, allowNull: false },
			notes: { type: DataTypes.STRING, allowNull: true },
			follow_via_email: { type: DataTypes.BOOLEAN, allowNull: true },

			// New field for workflow integration (added via migration)
			workflow_id: { type: DataTypes.INTEGER, allowNull: true, defaultValue: null },

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

	/**
	 * Associates the AbsenceClarification model with other models.
	 * @param {object} models - The models to associate with.
	 */
	AbsenceClarification.associate = (models) => {
		/**
		 * Associates the AbsenceClarification model with the AbsenceClarificationChild model.
		 * A AbsenceClarification has many AbsenceClarificationChilds.
		 * @param {object} models - The models to associate with.
		 */
		AbsenceClarification.hasMany(models.AbsenceClarificationChild, { foreignKey: "absenceclarification_id", as: "absenceclarificationchilds" });

		/**
		 * Associates the AbsenceClarification model with the Company model.
		 * A AbsenceClarification belongs to a Company.
		 * @param {object} models - The models to associate with.
		 */
		AbsenceClarification.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

		/**
		 * Associates the AbsenceClarification model with the Employee model.
		 * A AbsenceClarification belongs to an Employee.
		 * @param {object} models - The models to associate with.
		 */
		AbsenceClarification.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });
	};

	return AbsenceClarification;
};
