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

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

			// main
			disabled: { type: DataTypes.BOOLEAN, allowNull: false },
			name: { type: DataTypes.STRING(255), allowNull: false },
			account_name: { type: DataTypes.STRING(50), allowNull: true },
			account_number: { type: DataTypes.STRING(50), allowNull: true },
			is_group: { type: DataTypes.BOOLEAN, allowNull: true },
			root_type: { type: DataTypes.STRING(50), allowNull: true },
			report_type: { type: DataTypes.STRING(50), allowNull: true },
			account_type: { type: DataTypes.STRING(50), allowNull: true },
			account_currency: { type: DataTypes.STRING(50), allowNull: true },

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

	Account.belongsTo(Account, { as: "parent", foreignKey: "parent_account" });

	Account.associate = (models) => {
		Account.belongsTo(models.Company, { as: "company", foreignKey: "company_id" });
		Account.belongsTo(models.User, { as: "modifier", foreignKey: "modified_by" });
	};

	return Account;
};
