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

module.exports = (sequelize) => {
	const BudgetItem = sequelize.define(
		"budgetitem",
		{
			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 },
			item_code: { type: DataTypes.STRING(50), allowNull: true },
			item_name: { type: DataTypes.STRING, allowNull: false },
			specifications: { type: DataTypes.TEXT, allowNull: true },
			description: { type: DataTypes.TEXT, allowNull: true },
			unit_of_measurement: { type: DataTypes.STRING(50), allowNull: true },
			default_price: { type: DataTypes.FLOAT, allowNull: true },

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

	BudgetItem.associate = (models) => {
		BudgetItem.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		BudgetItem.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		BudgetItem.belongsTo(models.BudgetClassification, { foreignKey: "budgetclassification_id", as: "budgetclassification" });
		BudgetItem.belongsTo(models.BudgetCategory, { foreignKey: "budgetcategory_id", as: "budgetcategory" });
		BudgetItem.belongsTo(models.BudgetAccount, { foreignKey: "budgetaccount_id", as: "budgetaccount" });
		BudgetItem.belongsTo(models.BudgetDepartment, { foreignKey: "budgetdepartment_id", as: "budgetdepartment" });
		BudgetItem.belongsTo(models.BudgetUnitOfMeasurement, { foreignKey: "budgetunitofmeasurement_id", as: "budgetunitofmeasurement" });
	};

	return BudgetItem;
};
