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

module.exports = (sequelize) => {
	const Accessory = sequelize.define(
		'Accessory',
		{
			id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
			name: { type: DataTypes.STRING, allowNull: false },
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			category_id: { type: DataTypes.INTEGER, allowNull: true },
			manufacturer_id: { type: DataTypes.INTEGER, allowNull: true },
			location_id: { type: DataTypes.INTEGER, allowNull: true },
			model_number: { type: DataTypes.STRING, allowNull: true },
			qty: { type: DataTypes.INTEGER, defaultValue: 0 },
			min_amt: { type: DataTypes.INTEGER, defaultValue: 0 },
			purchase_date: { type: DataTypes.DATEONLY, allowNull: true },
			purchase_cost: { type: DataTypes.DECIMAL(13, 2), allowNull: true },
			order_number: { type: DataTypes.STRING, allowNull: true },
			notes: { type: DataTypes.TEXT, allowNull: true },
			image: { type: DataTypes.TEXT, allowNull: true },
			enabled: { type: DataTypes.TINYINT, defaultValue: 1 },
			creation: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
			modified: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
			owner: { type: DataTypes.INTEGER, allowNull: true },
			modified_by: { type: DataTypes.INTEGER, allowNull: true }
		},
		{
			tableName: 'accessories',
			timestamps: false
		}
	);

	Accessory.associate = (models) => {
		Accessory.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
		Accessory.belongsTo(models.AssetManufacturer, { foreignKey: 'manufacturer_id', as: 'manufacturer' });
		Accessory.belongsTo(models.AssetCategory, { foreignKey: 'category_id', as: 'category' });
		Accessory.belongsTo(models.AssetLocation, { foreignKey: 'location_id', as: 'location' });
		Accessory.hasMany(models.AccessoryCheckout, { foreignKey: 'accessory_id', as: 'accessoryCheckouts' });
	};

	return Accessory;
};
