const bcrypt = require('bcryptjs');
const { BOOLEAN, TEXT, ENUM, STRING, UUID, UUIDV4, BIGINT } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
	const Members = sequelize.define(
		'Members',
		{
			id: {
				type: UUID,
				defaultValue: UUIDV4,
				allowNull: false,
				primaryKey: true,
			},
			account_id: {
				type: BIGINT,
				allowNull: true,
				unique: true,
			},
			organization_name: {
				type: STRING,
				allowNull: true,
			},
			is_jci_senator: {
				type: ENUM('Yes', 'No'),
				allowNull: false,
			},
			local_org_name: {
				type: STRING,
				allowNull: true,
			},
			first_name: {
				type: STRING,
				allowNull: false,
			},
			last_name: {
				type: STRING,
				allowNull: false,
			},
			email_address: {
				type: STRING,
				allowNull: false,
				unique: true,
				validate: {
					isEmail: true,
				},
			},
			phone_number: {
				type: STRING(15),
				allowNull: true,
			},
			password: {
				type: STRING,
				allowNull: false,
			},
			is_tnc_agreed: {
				type: BOOLEAN,
				allowNull: false,
				defaultValue: false,
			},
			status: {
				type: BOOLEAN,
				allowNull: false,
				defaultValue: false,
			},
			profile_image: {
				type: TEXT,
				allowNull: true,
			},
			location: {
				type: TEXT,
				allowNull: true,
			},
			position: {
				type: STRING(100),
				allowNull: true,
			},
			cover_image: {
				type: TEXT,
				allowNull: true,
			},
			bio: {
				type: TEXT,
				allowNull: true,
			},
			birth_date: {
				type: DataTypes.DATEONLY,
				allowNull: true,
			},
			middle_name: {
				type: STRING,
				allowNull: true,
			},
			nationality: {
				type: STRING,
				allowNull: true,
			},
			food_type: {
				type: STRING,
				allowNull: true,
			},
			gender: {
				type: ENUM('Male', 'Female'),
				allowNull: true,
			},
			allergy: {
				type: STRING,
				allowNull: true,
			},
			country: {
				type: STRING,
				allowNull: true,
			},
			verified: {
				type: BOOLEAN,
				allowNull: false,
				defaultValue: false,
			},
		},
		{
			tableName: 'members',
			timestamps: true,
			hooks: {
				beforeCreate: async (customer) => {
					if (customer.changed('password')) {
						customer.password = await bcrypt.hash(customer.password, 10);
					}
				},
				beforeUpdate: async (customer) => {
					if (customer.changed('password')) {
						customer.password = await bcrypt.hash(customer.password, 10);
					}
				},
			},
		},
	);

	Members.prototype.validatePassword = async function (password) {
		return bcrypt.compare(password, this.password);
	};

	Members.associate = (models) => {
		// Members.hasOne(models.CustomerFlags, {
		// 	foreignKey: 'customer_id',
		// 	as: 'flags',
		// 	onDelete: 'CASCADE',
		// });

		// Hotel associations
		Members.hasMany(models.HotelBookings, {
			foreignKey: 'member_id',
			as: 'hotelBookings',
		});

		Members.hasMany(models.HotelReviews, {
			foreignKey: 'member_id',
			as: 'hotelReviews',
		});

		Members.hasMany(models.LinkedTickets, {
			foreignKey: 'member_id',
			as: 'linkedTickets',
		});
	};

	return Members;
};
