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

module.exports = (sequelize) => {
	const License = sequelize.define(
		'License',
		{
			id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
			name: { type: DataTypes.STRING, allowNull: false },
			license_key: { type: DataTypes.TEXT, allowNull: true },
			license_email: { type: DataTypes.STRING, allowNull: true },
			seats: { type: DataTypes.INTEGER, defaultValue: 1 },
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			manufacturer_id: { type: DataTypes.INTEGER, allowNull: true },
			category_id: { type: DataTypes.INTEGER, allowNull: true },
			order_number: { type: DataTypes.STRING, allowNull: true },
			purchase_date: { type: DataTypes.DATEONLY, allowNull: true },
			purchase_cost: { type: DataTypes.DECIMAL(13, 2), allowNull: true },
			expiration_date: { type: DataTypes.DATEONLY, allowNull: true },
			notes: { 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: 'licenses',
			timestamps: false
		}
	);

	License.associate = (models) => {
		License.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
		License.belongsTo(models.AssetManufacturer, { foreignKey: 'manufacturer_id', as: 'manufacturer' });
		License.belongsTo(models.AssetCategory, { foreignKey: 'category_id', as: 'category' });
		License.hasMany(models.LicenseSeat, { foreignKey: 'license_id', as: 'licenseSeats' });
	};

	return License;
};
