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

module.exports = (sequelize) => {
	const WorkActivityItem = sequelize.define(
		"workactivityitem",
		{
			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
			activity_date: { type: DataTypes.DATEONLY, allowNull: true }, // Tanggal aktivitas (untuk dokumen bulanan)
			work_type: { type: DataTypes.STRING, allowNull: true }, // WFO | WFH
			workactivity_plan: { type: DataTypes.TEXT, allowNull: true }, // Rencana Kerja
			workactivity_result: { type: DataTypes.TEXT, allowNull: true }, // Hasil / Tindak Lanjut
			sequence_number: { type: DataTypes.INTEGER, allowNull: true, defaultValue: 1 }, // No. (urutan per hari)
			file: { type: DataTypes.STRING, allowNull: true }, // Legacy: satu file. Prefer workactivityitemfiles (table workactivityitemfiles).

			// fk
			workactivity_id: { type: DataTypes.INTEGER, allowNull: false },
		},
		{
			tableName: "workactivityitems",
			freezeTableName: true,
		},
	);

	/**
	 * Associates the WorkActivityItem model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	WorkActivityItem.associate = (models) => {
		WorkActivityItem.belongsTo(models.WorkActivity, {
			foreignKey: "workactivity_id",
			as: "workactivity",
			onDelete: "CASCADE",
		});
		WorkActivityItem.hasMany(models.WorkActivityItemFile, {
			foreignKey: "workactivityitem_id",
			as: "workactivityitemfiles",
			onDelete: "CASCADE",
		});
	};

	return WorkActivityItem;
};
