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

module.exports = (sequelize) => {
	const BudgetClassification = sequelize.define(
		"budgetclassification",
		{
			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
			name: { type: DataTypes.STRING(150), allowNull: false },
			code: { type: DataTypes.STRING(50), allowNull: false },
			display_name: { type: DataTypes.STRING(50), allowNull: false },
			description: { type: DataTypes.TEXT, allowNull: true },
			color: { type: DataTypes.STRING(50), allowNull: true },

			// fk
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetdepartment_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetcategory_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "budgetclassifications",
			freezeTableName: true,
		}
	);

	BudgetClassification.associate = (models) => {
		BudgetClassification.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		BudgetClassification.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		BudgetClassification.belongsTo(models.BudgetCategory, { foreignKey: "budgetcategory_id", as: "budgetcategory" });
		BudgetClassification.belongsTo(models.BudgetDepartment, { foreignKey: "budgetdepartment_id", as: "budgetdepartment" });
	};

	return BudgetClassification;
};
