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

module.exports = (sequelize) => {
	const BudgetingItem = sequelize.define(
		"budgetingitem",
		{
			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
			account: { type: DataTypes.STRING(50), allowNull: true },
			budget_amount: { type: DataTypes.FLOAT, allowNull: true },

			item_code: { type: DataTypes.STRING(150), allowNull: true },
			sku_code: { type: DataTypes.STRING(150), allowNull: true },
			alternative_sku_code: { type: DataTypes.STRING(150), allowNull: true },
			item_name: { type: DataTypes.STRING(150), allowNull: true },
			item_group: { type: DataTypes.STRING(150), allowNull: true },
			stock_uom: { type: DataTypes.STRING(50), 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
			budgeting_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 },
		},
		{
			tableName: "budgetingitems",
			freezeTableName: true,
		}
	);

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

	return BudgetingItem;
};
