module.exports = (sequelize, DataTypes) => {
	const ProductPurchases = sequelize.define(
		'ProductPurchases',
		{
			id: {
				type: DataTypes.UUID,
				defaultValue: DataTypes.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			member_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
				references: {
					model: 'users',
					key: 'id',
				},
			},
			reference_no: {
				type: DataTypes.STRING,
				allowNull: false,
				unique: true,
			},
			status: {
				type: DataTypes.ENUM('Unpaid', 'Successful', 'Failed'),
				allowNull: true,
				defaultValue: 'Unpaid',
			},
			payment: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: true,
				defaultValue: 0.0,
			},
			payment_method: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			price: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: false,
				defaultValue: 0.0,
			},
			service_fee: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: true,
				defaultValue: 0.0,
			},
			taxes: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: true,
				defaultValue: 0.0,
			},
			total: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: true,
				defaultValue: 0.0,
			},
			payment_status: {
				type: DataTypes.ENUM('Paid', 'Partially Paid', 'Unpaid'),
				allowNull: false,
				defaultValue: 'Unpaid',
			},
			createdAt: {
				type: DataTypes.DATE,
				allowNull: false,
				defaultValue: DataTypes.NOW,
			},
			updatedAt: {
				type: DataTypes.DATE,
				allowNull: false,
				defaultValue: DataTypes.NOW,
			},
		},
		{
			tableName: 'product_purchases',
			timestamps: true,
		},
	);

	ProductPurchases.associate = (models) => {
		ProductPurchases.belongsTo(models.Members, {
			foreignKey: 'member_id',
			as: 'member',
		});
		ProductPurchases.hasMany(models.ProductPurchaseItems, {
			foreignKey: 'purchase_id',
			as: 'items',
		});
	};

	return ProductPurchases;
};
