const { TEXT, UUID, UUIDV4 } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
	const LinkedTickets = sequelize.define(
		'LinkedTickets',
		{
			id: {
				type: UUID,
				defaultValue: UUIDV4,
				allowNull: false,
				primaryKey: true,
			},
			member_id: {
				type: UUID,
				allowNull: false,
			},
			ticket_no: {
				type: TEXT,
				allowNull: false,
			},
		},
		{
			tableName: 'linked_tickets',
			timestamps: true,
		},
	);

	LinkedTickets.associate = (models) => {
		LinkedTickets.belongsTo(models.Members, {
			as: 'member',
			foreignKey: 'member_id',
		});
		LinkedTickets.belongsTo(models.Tickets, {
			as: 'ticket',
			foreignKey: 'ticket_no',
			targetKey: 'ticket_no',
		});
	};

	return LinkedTickets;
};
