module.exports = (sequelize, DataTypes) => {
	const Tickets = sequelize.define(
		'Tickets',
		{
			id: {
				type: DataTypes.UUID,
				defaultValue: DataTypes.UUIDV4,
				allowNull: false,
				primaryKey: true,
			},
			reference_no: {
				type: DataTypes.STRING,
				allowNull: false,
			},
			ticket_no: {
				type: DataTypes.STRING,
				allowNull: false,
				unique: true,
			},
			is_used: {
				type: DataTypes.BOOLEAN,
				allowNull: false,
				defaultValue: false,
			},
			scanned_datetime: {
				type: DataTypes.DATE,
				allowNull: true,
			},
			scanned_by: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			purchased_at: {
				type: DataTypes.DATE,
				allowNull: true,
				defaultValue: DataTypes.NOW,
			},
			purchased_by: {
				type: DataTypes.UUID,
				allowNull: true,
			},
			worldcon_ticket_id: {
				type: DataTypes.UUID,
				allowNull: true,
			},
			total: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: true,
				defaultValue: 0.0,
			},
		},
		{
			tableName: 'tickets',
			timestamps: false,
		},
	);

	Tickets.associate = (models) => {
		Tickets.hasOne(models.LinkedTickets, {
			as: 'linked',
			foreignKey: 'ticket_no',
			sourceKey: 'ticket_no',
		});
		Tickets.belongsTo(models.WorldConTickets, {
			as: 'worldConTicket',
			foreignKey: 'worldcon_ticket_id',
			targetKey: 'id',
		});
		Tickets.belongsTo(models.Members, {
			as: 'purchasedBy',
			foreignKey: 'purchased_by',
			targetKey: 'id',
		});
	};

	return Tickets;
};
