/**
 * Blocked IPs Model
 * Tracks IPs that frequently fail or are blocked
 */

module.exports = (sequelize, DataTypes) => {
	const BlockedIP = sequelize.define(
		'blocked_ips',
		{
			id: {
				type: DataTypes.INTEGER,
				primaryKey: true,
				autoIncrement: true,
			},
			ip_address: {
				type: DataTypes.STRING(45),
				allowNull: false,
			},
			isp_name: {
				type: DataTypes.STRING(50),
				allowNull: true,
			},
			block_reason: {
				type: DataTypes.STRING(255),
				allowNull: true,
			},
			first_blocked: {
				type: DataTypes.DATE,
				defaultValue: DataTypes.NOW,
				allowNull: false,
			},
			last_attempt: {
				type: DataTypes.DATE,
				defaultValue: DataTypes.NOW,
				allowNull: false,
			},
			attempt_count: {
				type: DataTypes.INTEGER,
				defaultValue: 1,
			},
			status: {
				type: DataTypes.ENUM('temporary', 'permanent', 'resolved', 'whitelisted'),
				defaultValue: 'temporary',
			},
			notes: {
				type: DataTypes.TEXT,
				allowNull: true,
			},
		},
		{
			timestamps: false,
			tableName: 'blockedips',
			indexes: [
				{
					fields: ['ip_address'],
				},
				{
					fields: ['status'],
				},
			],
		}
	);

	return BlockedIP;
};
