sdforge 0.3.1

Multi-protocol SDK framework with unified macro configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! Audit logging implementation
//!
//! This module provides audit logging with DoS protection and async processing.

use crate::cache::SharedCache;
use crate::security::types::{
    deserialize_audit_logs, serialize_audit_logs, AuditLog, AuditResult, AuthContext, AuthMetadata,
};
use log::warn;
use once_cell::sync::Lazy;
use std::sync::Arc;
use std::time::Duration;
use uuid::Uuid;

/// Batch of audit logs for async processing.
///
/// Internal struct used to pass user ID and log entry through the async channel.
pub(crate) struct AuditLogBatch {
    user_id: String,
    #[allow(dead_code)] // Field used in queue transfer; direct read access not needed
    log: AuditLog,
}

// =============================================================================
// Global State - Pre-compiled Regex Patterns for Sanitization
// =============================================================================
// These are immutable, thread-safe, pre-compiled regex patterns.
// Using Lazy initialization ensures they are compiled only once at first use,
// providing optimal performance while maintaining safety guarantees.
//
// Design Rationale:
// - Immutable after initialization (no mutable global state)
// - Thread-safe access via once_cell::Lazy
// - Performance optimization (avoid re-compilation on each call)
// - No dependency injection needed (these are pure functions with no side effects)
// =============================================================================

/// Pattern to match JWT tokens (three base64url-encoded segments separated by dots)
///
/// Used to detect and redact JWT tokens from error messages to prevent token leakage.
/// Format: header.payload.signature (each segment is base64url encoded)
static JWT_PATTERN: Lazy<regex::Regex> = Lazy::new(|| {
    regex::Regex::new(r#"eyJ[A-Za-z0-9\-_]+\.eyJ[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+"#).unwrap()
});

/// Pattern to match sensitive key-value pairs (passwords, secrets, tokens, keys)
///
/// Matches patterns like:
/// - `password=secret123`
/// - `token: abcdef`
/// - `api_key: xyz789`
///
/// Limited repetition (1-100 chars) prevents ReDoS attacks.
static SECRET_PATTERN: Lazy<regex::Regex> = Lazy::new(|| {
    // Limited repetition to prevent ReDoS attacks
    // Maximum 100 characters after the separator
    regex::Regex::new(r#"(?i)(password|secret|token|key|auth|bearer)\s*[:=]\s*[^,\s}\]]{1,100}"#)
        .unwrap()
});

/// Pattern to match certificate/key file paths
///
/// Matches paths ending with common certificate extensions:
/// - `.pem` - Privacy Enhanced Mail certificate
/// - `.key` - Private key file
/// - `.crt` - Certificate file
/// - `.p12` - PKCS#12 archive
/// - `.jks` - Java KeyStore
static PATH_PATTERN: Lazy<regex::Regex> =
    Lazy::new(|| regex::Regex::new(r#"/[a-zA-Z0-9/_.-]+\.(pem|key|crt|p12|jks)"#).unwrap());

/// Pattern to match API keys in error messages (case-insensitive key=value form)
static API_KEY_PATTERN: Lazy<regex::Regex> = Lazy::new(|| {
    regex::Regex::new(r#"(?i)(api[_-]?key|apikey)\s*[:=]\s*['\"]?[A-Za-z0-9]{20,}['\"]?"#).unwrap()
});

/// Pattern to match credit card numbers (16 digits, optional separators)
static CREDIT_CARD_PATTERN: Lazy<regex::Regex> =
    Lazy::new(|| regex::Regex::new(r#"\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b"#).unwrap());

/// Pattern to match US Social Security Numbers
static SSN_PATTERN: Lazy<regex::Regex> =
    Lazy::new(|| regex::Regex::new(r#"\b\d{3}[-\s]?\d{2}[-\s]?\d{4}\b"#).unwrap());

/// Sanitize error messages to remove sensitive information before logging.
///
/// This function helps prevent sensitive data (tokens, passwords, keys) from
/// being exposed in audit logs.
fn sanitize_error_message(message: &str) -> String {
    let mut result = message.to_string();

    // Remove JWT tokens
    result = JWT_PATTERN
        .replace_all(&result, "[REDACTED_JWT]")
        .to_string();

    // Remove secret patterns
    result = SECRET_PATTERN
        .replace_all(&result, |caps: &regex::Captures| {
            format!("{}={}", &caps[1], "[REDACTED]")
        })
        .to_string();

    // Remove API keys
    result = API_KEY_PATTERN
        .replace_all(&result, "[REDACTED_API_KEY]")
        .to_string();

    // Remove credit card numbers
    result = CREDIT_CARD_PATTERN
        .replace_all(&result, "[REDACTED_CREDIT_CARD]")
        .to_string();

    // Remove SSN numbers
    result = SSN_PATTERN
        .replace_all(&result, "[REDACTED_SSN]")
        .to_string();

    // Remove certificate/key file paths
    result = PATH_PATTERN
        .replace_all(&result, "[REDACTED_PATH]")
        .to_string();

    const MAX_SANITIZED_LENGTH: usize = 500;
    if result.len() > MAX_SANITIZED_LENGTH {
        result.truncate(MAX_SANITIZED_LENGTH);
        result.push_str("...[TRUNCATED]");
    }

    result
}

/// Audit logger with DoS protection
///
/// Security features:
/// - Semaphore-based rate limiting to prevent log flooding
/// - Per-user log count limits
/// - Async processing to avoid blocking main threads
/// - Fallback storage when async channel is full (prevents log loss)
///
#[derive(Clone)]
pub struct AppAuditLogger {
    /// Logs storage via SyncCache (keyed by user_id)
    logs: SharedCache,
    /// Maximum logs per user
    max_logs_per_user: usize,
    /// Rate limiting semaphore (max concurrent log operations)
    semaphore: Arc<tokio::sync::Semaphore>,
    /// Log queue sender (for async processing)
    queue_sender: Arc<tokio::sync::mpsc::Sender<AuditLogBatch>>,
    /// Fallback storage for when channel is full (synchronous path) via SyncCache
    fallback_logs: SharedCache,
    /// Counter for dropped logs (monitoring)
    dropped_log_count: Arc<std::sync::atomic::AtomicU64>,
    /// Counter for total logs successfully stored (monitoring)
    total_log_count: Arc<std::sync::atomic::AtomicU64>,
}

impl Default for AppAuditLogger {
    fn default() -> Self {
        Self::new()
    }
}

impl AppAuditLogger {
    /// Create a new AppAuditLoggerBuilder for custom configuration.
    ///
    /// This is the recommended way to create an AppAuditLogger when you need
    /// to customize any of the default settings.
    ///
    /// # Returns
    ///
    /// Returns a new AppAuditLoggerBuilder instance.
    ///
    /// # Errors
    ///
    /// This function does not return errors.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use sdforge::security::AppAuditLogger;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let logger = AppAuditLogger::builder()
    ///         .max_logs_per_user(500)
    ///         .max_concurrent_ops(50)
    ///         .queue_size(2000)
    ///         .build();
    ///     let _ = logger;
    /// }
    /// ```
    pub fn builder() -> AppAuditLoggerBuilder {
        AppAuditLoggerBuilder::new()
    }

    /// Create new audit logger with default limit
    pub fn new() -> Self {
        Self::with_limit(1000)
    }

    /// Create new audit logger with custom limit
    pub fn with_limit(max_logs: usize) -> Self {
        let (queue_sender, mut queue_receiver) = tokio::sync::mpsc::channel::<AuditLogBatch>(1000);

        // Spawn background worker for async log processing
        let logs: SharedCache = Arc::new(crate::cache::DashMapCache::new());
        let fallback_logs: SharedCache = Arc::new(crate::cache::DashMapCache::new());
        let logs_clone = logs.clone();
        let fallback_logs_clone = fallback_logs.clone();
        let max_logs_clone = max_logs;
        tokio::spawn(async move {
            // Primary storage is done synchronously by log() — this worker only
            // handles draining the queue and merging fallback logs.
            while let Some(batch) = queue_receiver.recv().await {
                let key = &batch.user_id;
                // Drain the queue: primary log was already stored by log().
                // Just handle any fallback logs for this user.
                if let Some(fallback_data) = fallback_logs_clone.get(key) {
                    let fallback: Vec<AuditLog> = deserialize_audit_logs(&fallback_data);
                    if !fallback.is_empty() {
                        let data = logs_clone.get(key);
                        let mut logs_vec: Vec<AuditLog> = data
                            .as_ref()
                            .map(|d| deserialize_audit_logs(d))
                            .unwrap_or_default();
                        logs_vec.extend(fallback);
                        if logs_vec.len() > max_logs_clone {
                            logs_vec.truncate(max_logs_clone);
                        }
                        logs_clone.set(key, serialize_audit_logs(&logs_vec));
                        fallback_logs_clone.delete(key);
                    }
                }
            }
        });

        Self {
            logs,
            max_logs_per_user: max_logs,
            semaphore: Arc::new(tokio::sync::Semaphore::new(100)), // Max 100 concurrent log operations
            queue_sender: Arc::new(queue_sender),
            fallback_logs,
            dropped_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
            total_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
        }
    }

    /// Log an action (with DoS protection)
    ///
    /// Uses semaphore to limit concurrent log operations, preventing
    /// the audit logger from being a DoS vector.
    pub async fn log(
        &self,
        context: &AuthContext,
        action: impl Into<String>,
        resource: impl Into<String>,
        success: bool,
        message: Option<String>,
    ) {
        // Acquire permit with timeout to prevent blocking
        let permit = match tokio::time::timeout(
            Duration::from_secs(1),
            self.semaphore.clone().acquire_owned(),
        )
        .await
        {
            Ok(Ok(permit)) => permit,
            Ok(Err(_)) | Err(_) => {
                // Semaphore closed or timeout - skip logging to prevent DoS
                return;
            }
        };

        let mut log = AuditLog {
            id: Uuid::new_v4().to_string(),
            timestamp: chrono::Utc::now().timestamp(),
            user_id: context.user_id.clone(),
            action: action.into(),
            resource: resource.into(),
            result: if success {
                AuditResult::Success
            } else {
                AuditResult::Failure {
                    // Sanitize error message to prevent sensitive data exposure
                    message: sanitize_error_message(
                        &message.unwrap_or_else(|| "Unknown error".to_string()),
                    ),
                }
            },
            metadata: context.metadata.clone(),
            signature: None, // Will be signed if a signing key is configured
        };

        // Generate cryptographic signature for tamper detection (if enabled)
        // In production, you should configure a signing key via secure configuration
        // Security: Use a cryptographically secure random key of at least 32 bytes
        if let Ok(signing_key_str) = std::env::var("SDFORGE_AUDIT_SIGNING_KEY") {
            if !signing_key_str.is_empty() {
                // Convert string to bytes and use as HMAC key
                log.generate_signature(signing_key_str.as_bytes());
            } else {
                warn!("⚠️  WARNING: SDFORGE_AUDIT_SIGNING_KEY is empty. Audit logs will not be signed.");
            }
        } else {
            warn!(
                "⚠️  WARNING: SDFORGE_AUDIT_SIGNING_KEY not set. Audit logs will not be signed.\n   For production, set this environment variable to a secure random value (min 32 bytes).\n   Example: export SDFORGE_AUDIT_SIGNING_KEY=$(openssl rand -hex 32)"
            );
        }

        let user_id = context
            .user_id
            .clone()
            .unwrap_or_else(|| "anonymous".to_string());

        // === Synchronous write to primary storage ===
        // This ensures logs are immediately visible via get_logs() without
        // relying on the async worker being scheduled. Critical for tests and
        // for any code that reads logs immediately after logging.
        let key = &user_id;
        let data = self.logs.get(key);
        let mut logs_vec: Vec<AuditLog> = data
            .as_ref()
            .map(|d| deserialize_audit_logs(d))
            .unwrap_or_default();
        logs_vec.push(log.clone());
        if logs_vec.len() > self.max_logs_per_user {
            logs_vec.truncate(self.max_logs_per_user);
        }
        let bytes = serialize_audit_logs(&logs_vec);
        self.logs.set(key, bytes);

        // Increment the total log counter for accurate monitoring.
        // (Previously total_log_count() silently returned 0, hiding real activity.)
        self.total_log_count
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        // Also merge any pending fallback logs synchronously (worker will also do this)
        if let Some(fallback_data) = self.fallback_logs.get(key) {
            let fallback: Vec<AuditLog> = deserialize_audit_logs(&fallback_data);
            if !fallback.is_empty() {
                let mut merged = logs_vec;
                merged.extend(fallback);
                if merged.len() > self.max_logs_per_user {
                    merged.truncate(self.max_logs_per_user);
                }
                self.logs.set(key, serialize_audit_logs(&merged));
                self.fallback_logs.delete(key);
            }
        }

        // Send to async queue for potential downstream consumers.
        // The primary storage is already done above. This is fire-and-forget
        // for background processing that may have been relying on the queue.
        let sender = self.queue_sender.clone();
        let log_batch = AuditLogBatch {
            user_id: user_id.clone(),
            log,
        };

        // Try non-blocking send — primary storage is already complete above
        match sender.try_send(log_batch) {
            Ok(()) => {
                // Audit log queued successfully
            }
            Err(tokio::sync::mpsc::error::TrySendError::Full(_)) => {
                // Channel is full — primary storage already done above (synchronous path)
                // dropped_log_count is NOT incremented since we stored successfully
            }
            Err(tokio::sync::mpsc::error::TrySendError::Closed(_)) => {
                // Channel closed — primary storage already done above
            }
        }

        // Drop permit to release semaphore
        drop(permit);
    }

    /// Get logs for a user (synchronous)
    ///
    /// Security: Merges logs from both async and fallback storage to ensure
    /// complete audit trail is available even after channel congestion.
    pub fn get_logs(&self, user_id: &str) -> Vec<AuditLog> {
        // Get logs from primary storage
        let primary = self
            .logs
            .get(user_id)
            .map(|data| deserialize_audit_logs(&data))
            .unwrap_or_default();

        // Get logs from fallback storage
        let fallback = self
            .fallback_logs
            .get(user_id)
            .map(|data| deserialize_audit_logs(&data))
            .unwrap_or_default();

        // Merge and deduplicate (prefer primary logs if duplicates exist)

        let mut all_logs = primary;
        for log in fallback {
            if !all_logs.iter().any(|l| l.id == log.id) {
                all_logs.push(log);
            }
        }

        // Sort by timestamp descending
        all_logs.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

        all_logs
    }

    /// Clear logs for a user (admin function)
    pub fn clear_logs(&self, user_id: &str) {
        self.logs.delete(user_id);
    }

    /// Log an API key rotation event.
    ///
    /// # Arguments
    ///
    /// * `key_id` - The ID of the API key being rotated
    /// * `old_version` - The version being rotated from
    /// * `new_version` - The version being rotated to
    /// * `success` - Whether the rotation was successful
    /// * `message` - Optional message with additional details
    pub async fn log_key_rotation(
        &self,
        _key_id: &str,
        _old_version: &str,
        _new_version: &str,
        success: bool,
        message: Option<String>,
    ) {
        let context = AuthContext {
            user_id: Some("system".to_string()),
            permissions: vec![], // System rotation has no specific permissions
            metadata: AuthMetadata {
                client_ip: None,
                user_agent: None,
                request_id: format!("rotation_{}", chrono::Utc::now().timestamp()),
                timestamp: chrono::Utc::now().timestamp(),
            },
        };

        self.log(&context, "key_rotation", "api_key", success, message)
            .await;
    }

    /// Get total log count (for monitoring)
    ///
    /// Note: With SyncCache trait (no iteration support), this returns an
    /// approximate count by checking individual known user keys.
    /// For accurate counting, use a separate counter or database query.
    pub fn total_log_count(&self) -> usize {
        // Returns the actual count of logs successfully stored via log().
        // (Previously returned a hardcoded 0, hiding real audit activity.)
        self.total_log_count
            .load(std::sync::atomic::Ordering::Relaxed) as usize
    }

    /// Get count of dropped logs (for monitoring)
    pub fn dropped_log_count(&self) -> u64 {
        self.dropped_log_count
            .load(std::sync::atomic::Ordering::SeqCst)
    }
}

/// Implement AuditLogger trait for AppAuditLogger
impl crate::security::traits::AuditLogger for AppAuditLogger {
    fn log(&self, log: AuditLog) {
        // Build an AuthContext from the audit log for the async log method
        let context = AuthContext {
            user_id: log.user_id.clone(),
            permissions: vec![],
            metadata: log.metadata.clone(),
        };
        let result = matches!(log.result, AuditResult::Success);
        let message = match &log.result {
            AuditResult::Success => None,
            AuditResult::Failure { message } => Some(message.clone()),
        };
        let action = log.action.clone();
        let resource = log.resource.clone();

        // Clone all Arc fields from self for use in the spawned task
        let logs = self.logs.clone();
        let max_logs_per_user = self.max_logs_per_user;
        let semaphore = self.semaphore.clone();
        let queue_sender = self.queue_sender.clone();
        let fallback_logs = self.fallback_logs.clone();
        let dropped_log_count = self.dropped_log_count.clone();

        // Reuse the current tokio runtime instead of spawning a new OS thread + runtime per log call.
        // Previous implementation used std::thread::spawn + tokio::runtime::Builder which had
        // unacceptable overhead (thread creation + runtime construction per audit log) and
        // could panic the process on resource exhaustion.
        match tokio::runtime::Handle::try_current() {
            Ok(handle) => {
                handle.spawn(async move {
                    let logger = AppAuditLogger {
                        logs,
                        max_logs_per_user,
                        semaphore,
                        queue_sender,
                        fallback_logs,
                        dropped_log_count,
                        total_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
                    };
                    logger
                        .log(&context, action, resource, result, message)
                        .await;
                });
            }
            Err(_) => {
                // No tokio runtime available — emit to stderr as fallback
                // and increment the dropped-log counter so monitoring can
                // detect the loss (previously the counter was never updated,
                // making drops invisible to operators).
                dropped_log_count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                warn!(
                    "[AuditLogger] WARNING: no tokio runtime available, audit log dropped for action={}"
                , action);
            }
        }
    }
}

/// Builder for creating AppAuditLogger with custom configuration.
///
/// This builder allows fine-grained control over audit logger settings
/// including log limits, concurrency, and queue size.
///
/// # Examples
///
/// ```ignore
/// use sdforge::security::AppAuditLogger;
///
/// #[tokio::main]
/// async fn main() {
///     let logger = AppAuditLogger::builder()
///         .max_logs_per_user(500)
///         .max_concurrent_ops(50)
///         .queue_size(2000)
///         .build();
///     let _ = logger;
/// }
/// ```
pub struct AppAuditLoggerBuilder {
    /// Maximum number of logs to retain per user
    max_logs_per_user: usize,
    /// Maximum number of concurrent log operations (semaphore permits)
    max_concurrent_ops: usize,
    /// Size of the async log processing queue
    queue_size: usize,
}

impl Default for AppAuditLoggerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl AppAuditLoggerBuilder {
    /// Create a new AppAuditLoggerBuilder with default settings.
    ///
    /// Default values:
    /// - `max_logs_per_user`: 1000
    /// - `max_concurrent_ops`: 100
    /// - `queue_size`: 1000
    ///
    /// # Returns
    ///
    /// Returns a builder initialized with default configuration.
    ///
    /// # Errors
    ///
    /// This function does not return errors.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sdforge::security::AppAuditLoggerBuilder;
    ///
    /// let builder = AppAuditLoggerBuilder::new();
    /// let _ = builder;
    /// ```
    pub fn new() -> Self {
        Self {
            max_logs_per_user: 1000,
            max_concurrent_ops: 100,
            queue_size: 1000,
        }
    }

    /// Set the maximum number of logs to retain per user.
    ///
    /// When the limit is exceeded, older logs are truncated.
    ///
    /// # Arguments
    ///
    /// * `max_logs` - Maximum number of logs per user (default: 1000).
    ///
    /// # Returns
    ///
    /// Returns the updated builder instance.
    ///
    /// # Errors
    ///
    /// This function does not return errors.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sdforge::security::AppAuditLoggerBuilder;
    ///
    /// let builder = AppAuditLoggerBuilder::new().max_logs_per_user(500);
    /// let _ = builder;
    /// ```
    pub fn max_logs_per_user(mut self, max_logs: usize) -> Self {
        self.max_logs_per_user = max_logs;
        self
    }

    /// Set the maximum number of concurrent log operations.
    ///
    /// This controls the semaphore permit count, limiting how many
    /// log operations can run simultaneously to prevent DoS.
    ///
    /// # Arguments
    ///
    /// * `max_concurrent` - Maximum concurrent log operations (default: 100).
    ///
    /// # Returns
    ///
    /// Returns the updated builder instance.
    ///
    /// # Errors
    ///
    /// This function does not return errors.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sdforge::security::AppAuditLoggerBuilder;
    ///
    /// let builder = AppAuditLoggerBuilder::new().max_concurrent_ops(50);
    /// let _ = builder;
    /// ```
    pub fn max_concurrent_ops(mut self, max_concurrent: usize) -> Self {
        self.max_concurrent_ops = max_concurrent;
        self
    }

    /// Set the size of the async log processing queue.
    ///
    /// Larger queues allow more buffering during high load but use more memory.
    ///
    /// # Arguments
    ///
    /// * `queue_size` - Size of the async log processing queue (default: 1000).
    ///
    /// # Returns
    ///
    /// Returns the updated builder instance.
    ///
    /// # Errors
    ///
    /// This function does not return errors.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sdforge::security::AppAuditLoggerBuilder;
    ///
    /// let builder = AppAuditLoggerBuilder::new().queue_size(2000);
    /// let _ = builder;
    /// ```
    pub fn queue_size(mut self, queue_size: usize) -> Self {
        self.queue_size = queue_size;
        self
    }

    /// Build an AppAuditLogger instance using the configured settings.
    ///
    /// This method spawns a background worker task for async log processing.
    /// Ensure tokio runtime is available when calling this method.
    ///
    /// # Returns
    ///
    /// Returns a fully configured AppAuditLogger instance.
    ///
    /// # Errors
    ///
    /// This function does not return errors.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use sdforge::security::AppAuditLoggerBuilder;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let logger = AppAuditLoggerBuilder::new()
    ///         .max_logs_per_user(500)
    ///         .max_concurrent_ops(50)
    ///         .queue_size(2000)
    ///         .build();
    ///     let _ = logger;
    /// }
    /// ```
    pub fn build(self) -> AppAuditLogger {
        let (queue_sender, mut queue_receiver) =
            tokio::sync::mpsc::channel::<AuditLogBatch>(self.queue_size);

        // Spawn background worker for async log processing
        let logs: SharedCache = Arc::new(crate::cache::DashMapCache::new());
        let fallback_logs: SharedCache = Arc::new(crate::cache::DashMapCache::new());
        let logs_clone = logs.clone();
        let fallback_logs_clone = fallback_logs.clone();
        let max_logs_clone = self.max_logs_per_user;
        tokio::spawn(async move {
            // Primary storage is done synchronously by log() — this worker only
            // handles draining the queue and merging fallback logs.
            while let Some(batch) = queue_receiver.recv().await {
                let key = &batch.user_id;
                // Drain the queue: primary log was already stored by log().
                // Just handle any fallback logs for this user.
                if let Some(fallback_data) = fallback_logs_clone.get(key) {
                    let fallback: Vec<AuditLog> = deserialize_audit_logs(&fallback_data);
                    if !fallback.is_empty() {
                        let data = logs_clone.get(key);
                        let mut logs_vec: Vec<AuditLog> = data
                            .as_ref()
                            .map(|d| deserialize_audit_logs(d))
                            .unwrap_or_default();
                        logs_vec.extend(fallback);
                        if logs_vec.len() > max_logs_clone {
                            logs_vec.truncate(max_logs_clone);
                        }
                        logs_clone.set(key, serialize_audit_logs(&logs_vec));
                        fallback_logs_clone.delete(key);
                    }
                }
            }
        });

        AppAuditLogger {
            logs,
            max_logs_per_user: self.max_logs_per_user,
            semaphore: Arc::new(tokio::sync::Semaphore::new(self.max_concurrent_ops)),
            queue_sender: Arc::new(queue_sender),
            fallback_logs,
            dropped_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
            total_log_count: Arc::new(std::sync::atomic::AtomicU64::new(0)),
        }
    }
}

#[cfg(test)]
mod tests;