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

module.exports = (sequelize) => {
	const ComponentAsset = sequelize.define(
		'ComponentAsset',
		{
			id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
			component_id: { type: DataTypes.INTEGER, allowNull: false },
			asset_id: { type: DataTypes.INTEGER, allowNull: false },
			note: { type: DataTypes.TEXT, allowNull: true },
			creation: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
			modified: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
			assigned_by: { type: DataTypes.INTEGER, allowNull: true }
		},
		{
			tableName: 'componentassets',
			timestamps: false
		}
	);

	ComponentAsset.associate = (models) => {
		ComponentAsset.belongsTo(models.Component, { foreignKey: 'component_id', as: 'component' });
		ComponentAsset.belongsTo(models.Asset, { foreignKey: 'asset_id', as: 'asset' });
	};

	return ComponentAsset;
};
