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

module.exports = (sequelize) => {
	const BudgetDepartment = sequelize.define(
		"budgetdepartment",
		{
			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 },
			disabled: { type: DataTypes.BOOLEAN, allowNull: false },

			name: { type: DataTypes.STRING(100), allowNull: false },
			department_name: { type: DataTypes.STRING(150), allowNull: false },
			is_group: { type: DataTypes.BOOLEAN, allowNull: true },
			leave_block_list: { type: DataTypes.STRING(150), allowNull: true },
			payroll_cost_center: { type: DataTypes.STRING(150), allowNull: true },

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

	BudgetDepartment.belongsTo(BudgetDepartment, { as: "parent", foreignKey: "parent_department" });

	BudgetDepartment.associate = (models) => {
		BudgetDepartment.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		BudgetDepartment.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		BudgetDepartment.hasMany(models.BudgetApprover, { foreignKey: "budgetdepartment_id", as: "budgetapprovers" });
	};

	return BudgetDepartment;
};
