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

module.exports = (sequelize) => {
	const EbudgetingItem = sequelize.define(
		"ebudgetingItem",
		{
			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
			name: { type: DataTypes.STRING(150), allowNull: false }, // e.g., "Budget Plan 2024 - Marketing Dept"
			display_name: { type: DataTypes.STRING(150), allowNull: false },
			description: { type: DataTypes.TEXT, allowNull: true },

			code: { type: DataTypes.STRING(50), allowNull: true }, // e.g., '4000-Expenses'
			name: { type: DataTypes.STRING(150), allowNull: true },
			account_name: { type: DataTypes.STRING(50), allowNull: true },
			account_number: { type: DataTypes.INTEGER, allowNull: true },
			account_type: { type: DataTypes.STRING(50), allowNull: true }, // e.g., 'Expense', 'Asset'
			root_type: { type: DataTypes.STRING(50), allowNull: true },

			item_id: { type: DataTypes.STRING(150), allowNull: true },
			item_name: { type: DataTypes.STRING(150), allowNull: true },
			qty: { type: DataTypes.FLOAT, allowNull: true },
			price: { type: DataTypes.FLOAT, allowNull: true },
			total_price: { type: DataTypes.FLOAT, allowNull: true },
			comment: { type: DataTypes.TEXT, allowNull: true },

			// fk
			uom_id: { type: DataTypes.STRING(50), allowNull: true },
			budgettype_id: { type: DataTypes.STRING(50), allowNull: true },
			budgetcategory_id: { type: DataTypes.STRING(50), allowNull: true },
			ebudgeting_id: { type: DataTypes.INTEGER, allowNull: true },

			employee_id: { type: DataTypes.INTEGER, allowNull: true },
			department_id: { type: DataTypes.INTEGER, allowNull: true },
			company_id: { type: DataTypes.INTEGER, allowNull: true },

			erpnext_id: { type: DataTypes.STRING(50), allowNull: true },
		},
		{
			tableName: "ebudgetingItems",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the EmployeeGroup model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	EbudgetingItem.associate = (models) => {
		EbudgetingItem.belongsTo(models.Company, { as: "company", foreignKey: "company_id" });
		EbudgetingItem.belongsTo(models.Department, { as: "department", foreignKey: "department_id" });
		EbudgetingItem.belongsTo(models.Ebudgeting, { as: "ebudgeting", foreignKey: "ebudgeting_id" });
		EbudgetingItem.belongsTo(models.Employee, { as: "employee", foreignKey: "employee_id" });
	};

	return EbudgetingItem;
};
