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

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

			// monthly doc support (1 doc = 1 bulan). Untuk dokumen bulanan, activity_date biasanya NULL.
			period_month: { type: DataTypes.INTEGER, allowNull: true },
			period_year: { type: DataTypes.INTEGER, allowNull: true },

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

	/**
	 * Associates the WorkActivity model with other models.
	 * @param {object} models - The models to associate with.
	 */
	WorkActivity.associate = (models) => {
		WorkActivity.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });
		WorkActivity.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		WorkActivity.hasMany(models.WorkActivityItem, { foreignKey: "workactivity_id", as: "workactivityitems" });
	};

	return WorkActivity;
};
