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

module.exports = (sequelize) => {
	const AssetLoanItem = sequelize.define(
		"assetloanitem",
		{
			id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
			creation: { type: DataTypes.DATE, allowNull: false },
			modified: { type: DataTypes.DATE, allowNull: false },
			createdAt: { type: DataTypes.DATE, allowNull: true, field: "createdAt" },
			updatedAt: { type: DataTypes.DATE, allowNull: true, field: "updatedAt" },

			loan_id: { type: DataTypes.INTEGER, allowNull: false },
			asset_id: { type: DataTypes.INTEGER, allowNull: false },
			notes: { type: DataTypes.TEXT, allowNull: true },
		},
		{
			tableName: "assetloanitems",
			freezeTableName: true,
			indexes: [
				{ name: "idx_assetloanitems_loan_id", fields: ["loan_id"] },
				{ name: "idx_assetloanitems_asset_id", fields: ["asset_id"] },
			],
		}
	);

	AssetLoanItem.associate = (models) => {
		AssetLoanItem.belongsTo(models.AssetLoan, { foreignKey: "loan_id", as: "loan" });
		AssetLoanItem.belongsTo(models.Asset, { foreignKey: "asset_id", as: "asset" });
	};

	return AssetLoanItem;
};
