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

module.exports = (sequelize) => {
	const BudgetAccount = sequelize.define(
		"budgetaccount",
		{
			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 },
			description: { type: DataTypes.STRING(250), allowNull: true },
			
			// fk
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetdepartment_id: { type: DataTypes.INTEGER, allowNull: true },
			account_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "budgetaccounts",
			freezeTableName: true,
		}
	);

	BudgetAccount.associate = (models) => {
		BudgetAccount.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		BudgetAccount.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });;
		BudgetAccount.belongsTo(models.Account, { foreignKey: "account_id", as: "account" });
		BudgetAccount.belongsTo(models.BudgetDepartment, { foreignKey: "budgetdepartment_id", as: "budgetdepartment" });
	};

	return BudgetAccount;
};
