/**
 * ISP Statistics Model
 * Aggregated statistics per ISP
 */

module.exports = (sequelize, DataTypes) => {
	const ISPStatistics = sequelize.define(
		'isp_statistics',
		{
			id: {
				type: DataTypes.INTEGER,
				primaryKey: true,
				autoIncrement: true,
			},
			isp_name: {
				type: DataTypes.STRING(50),
				allowNull: false,
				unique: true,
			},
			total_attempts: {
				type: DataTypes.INTEGER,
				defaultValue: 0,
			},
			successful_attempts: {
				type: DataTypes.INTEGER,
				defaultValue: 0,
			},
			failed_attempts: {
				type: DataTypes.INTEGER,
				defaultValue: 0,
			},
			timeout_count: {
				type: DataTypes.INTEGER,
				defaultValue: 0,
			},
			blocked_count: {
				type: DataTypes.INTEGER,
				defaultValue: 0,
			},
			avg_response_time_ms: {
				type: DataTypes.DECIMAL(10, 2),
				defaultValue: 0,
			},
			success_rate: {
				type: DataTypes.DECIMAL(5, 2),
				defaultValue: 0,
			},
			last_success: {
				type: DataTypes.DATE,
				allowNull: true,
			},
			last_failure: {
				type: DataTypes.DATE,
				allowNull: true,
			},
		},
		{
			timestamps: true,
			tableName: 'isp_statistics',
			createdAt: 'created_at',
			updatedAt: 'updated_at',
			indexes: [
				{
					unique: true,
					fields: ['isp_name'],
				},
			],
		}
	);

	return ISPStatistics;
};
