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

module.exports = (sequelize) => {
	const AssetModel = sequelize.define(
		"assetmodel",
		{
			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 },
			createdAt: { type: DataTypes.DATE, allowNull: true, field: "createdAt" },
			updatedAt: { type: DataTypes.DATE, allowNull: true, field: "updatedAt" },
			enabled: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
			name: { type: DataTypes.STRING(150), allowNull: false },
			display_name: { type: DataTypes.STRING(150), allowNull: false },
			description: { type: DataTypes.TEXT, allowNull: true },
			model_number: { type: DataTypes.STRING(100), allowNull: true },
			manufacturer_id: { type: DataTypes.INTEGER, allowNull: true },
			category_id: { type: DataTypes.INTEGER, allowNull: true },
			company_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "assetmodels",
			freezeTableName: true,
			indexes: [
				{ name: "idx_assetmodels_company_id", fields: ["company_id"] },
				{ name: "idx_assetmodels_enabled", fields: ["enabled"] },
				{ name: "idx_assetmodels_manufacturer_id", fields: ["manufacturer_id"] },
				{ name: "idx_assetmodels_category_id", fields: ["category_id"] },
			],
		}
	);

	AssetModel.associate = (models) => {
		AssetModel.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		AssetModel.belongsTo(models.AssetManufacturer, { foreignKey: "manufacturer_id", as: "manufacturer" });
		AssetModel.belongsTo(models.AssetCategory, { foreignKey: "category_id", as: "category" });
	};

	return AssetModel;
};
