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

module.exports = (sequelize) => {
	const Rides = sequelize.define(
		'Rides',
		{
			id: {
				type: DataTypes.INTEGER,
				autoIncrement: true,
				primaryKey: true,
			},
			member_id: {
				type: DataTypes.UUID,
				allowNull: false,
			},
			driver_id: {
				type: DataTypes.UUID,
				allowNull: false,
			},
			driver_route_id: {
				type: DataTypes.UUID,
				allowNull: false,
			},
			status: {
				type: DataTypes.ENUM('Ongoing', 'Completed', 'Cancelled'),
				defaultValue: 'Ongoing',
			},
			start_location: {
				type: DataTypes.STRING,
				allowNull: false,
			},
			end_location: {
				type: DataTypes.STRING,
				allowNull: false,
			},
			bus_number: {
				type: DataTypes.STRING,
				allowNull: false,
			},
			date: {
				type: DataTypes.DATEONLY,
				allowNull: false,
			},
			onboard_at: {
				type: DataTypes.DATE,
				allowNull: false,
			},
			offboard_at: {
				type: DataTypes.DATE,
				allowNull: true,
			},
		},
		{
			tableName: 'rides',
			timestamps: true,
		},
	);

	Rides.associate = (models) => {
		Rides.belongsTo(models.Members, {
			foreignKey: 'member_id',
			as: 'member',
		});

		Rides.belongsTo(models.Drivers, {
			foreignKey: 'driver_id',
			as: 'driver',
		});
	};

	return Rides;
};
