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

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

			name: { type: DataTypes.STRING(100), allowNull: false },
			display_name: { type: DataTypes.STRING(150), allowNull: false },
			slug_name: { type: DataTypes.STRING(100), allowNull: true },
			description: { type: DataTypes.TEXT, allowNull: true },
			code: { type: DataTypes.STRING(20), allowNull: true },
			type: { type: DataTypes.STRING(20), allowNull: true },
			is_parent: { type: DataTypes.BOOLEAN, allowNull: true },
			leave_block_list: { type: DataTypes.INTEGER, allowNull: true },

			// fk
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			parent_id: { type: DataTypes.INTEGER, allowNull: true },

			// sappsv1 or erpnext
			idx: { type: DataTypes.STRING, allowNull: true },
			ids: { type: DataTypes.INTEGER, allowNull: true },
			import_from: { type: DataTypes.STRING(100), allowNull: true },
		},
		{
			tableName: "departments",
		},
		{
			freezeTableName: true,
		}
	);

	/**
	 * Associates the Department model with other models.
	 */
	Department.associate = (models) => {
		/**
		 * Associates the Department model with the Company model.
		 */
		Department.belongsTo(models.Company, {
			foreignKey: "company_id",
			as: "company",
		});

		/**
		 * Associates the Department model with the Employee model.
		 */
		Department.hasMany(models.Employee, {
			foreignKey: "department_id",
			as: "departments",
		});

		/**
		 * Associates the Department model with the Designation model.
		 */
		Department.hasMany(models.Designation, {
			foreignKey: "department_id",
			as: "designations",
		});

		/**
		 * Associates the Department model with the EmployeeRequest model.
		 */
		Department.hasMany(models.EmployeeRequest, {
			foreignKey: "department_id",
			as: "departmentrequests",
		});

		/**
		 * Associates the Designation model with the EmployeeRequest model.
		 * A Designation has many EmployeeRequests where it is the required designation.
		 */
		Department.hasMany(models.WorkplanMonitor, {
			foreignKey: "owner_id",
			as: "owner_info",
		});

		/**
		 * Associates the Designation model with the EmployeeRequest model.
		 * A Designation has many EmployeeRequests where it is the superior designation
		 */
		Department.hasMany(models.WorkplanMonitor, {
			foreignKey: "counterpart_id",
			as: "counterpart_info",
		});

		/**
		 * Associates the Department model with the InternalAudit model.
		 * A Department has many InternalAudits.
		 */
		Department.hasMany(models.InternalAudit, {
			foreignKey: "department_id",
			as: "internalaudits",
		});

		/**
		 * Associates the Department model with the WorkInstruction model.
		 * A Department has many WorkInstructions.
		 */
		Department.hasMany(models.WorkInstruction, {
			foreignKey: "department_id",
			as: "workinstructions",
		});

		/**
		 * Associates the Department model with the LeaveApplication model.
		 * A Department has many LeaveApplications.
		 */
		Department.hasMany(models.LeaveApplication, {
			foreignKey: "department_id",
			as: "leaveapplications",
		});

		Department.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });

		// parent
		Department.belongsTo(models.Department, { foreignKey: "parent_id", as: "parent" });

		// child
		Department.hasMany(models.Department, { foreignKey: "parent_id", as: "children" });
	};

	return Department;
};
