//! Main run loop for the IngestWorker.
//!
//! Contains the continuous ingestion loop that tails the WAL.

use super::IngestWorker;
use crate::error::IngestError;
use std::sync::atomic::Ordering;
use stemedb_storage::KVStore;
use tracing::{debug, error, info, warn};

impl<S: KVStore + 'static> IngestWorker<S> {
    /// Run the continuous ingestion loop, tailing the WAL until shutdown.
    pub async fn run(&mut self) {
        info!("Starting ingestion loop...");
        loop {
            // Check for shutdown signal
            if self.shutdown.load(Ordering::Relaxed) {
                info!("Shutdown signal received, stopping ingestion loop");
                break;
            }

            match self.step().await {
                Ok(0) => {
                    // No new data, sleep briefly
                    tokio::time::sleep(std::time::Duration::from_millis(10)).await;
                }
                Ok(_) => {
                    // Processed data, continue immediately
                }
                Err(e) => {
                    // On shutdown, WAL errors are expected (files may be deleted)
                    if self.shutdown.load(Ordering::Relaxed) {
                        debug!("Error during shutdown (expected): {:?}", e);
                        break;
                    }
                    match &e {
                        IngestError::InputValidation(msg) => {
                            warn!("Rejected invalid input: {}", msg);
                        }
                        IngestError::InvalidSignature(msg) => {
                            warn!("Rejected invalid signature: {}", msg);
                        }
                        _ => {
                            error!("Ingestion error: {:?}", e);
                        }
                    }
                    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
                }
            }
        }
        info!("Ingestion loop stopped");
    }
}
