// ============================================================================
// Raw processor -- ported from processors/raw.ts
// ============================================================================
//
// Stores each chunk directly as a memory. The TS version also does:
// - Embedding computation
// - SimHash deduplication
// - Post-store job enqueueing
// These integrations will be wired up when the embedding and job systems
// are ported. For now, we store the memory directly via DB insert.

use crate::db::Database;
use crate::ingestion::types::{Chunk, ProcessOptions, ProcessResult};
use crate::EngError;
use uuid::Uuid;

fn rusqlite_to_eng_error(err: rusqlite::Error) -> EngError {
    EngError::DatabaseMessage(err.to_string())
}

/// Process chunks by storing each as a raw memory.
#[tracing::instrument(skip(db, chunks, options), fields(chunk_count = chunks.len()))]
pub async fn process(db: &Database, chunks: &[Chunk], options: &ProcessOptions) -> ProcessResult {
    let mut memories_created = 0;
    let mut errors = Vec::new();

    for chunk in chunks {
        let content = chunk.text.trim();
        if content.is_empty() {
            errors.push(format!("Chunk {}: empty after trim", chunk.index));
            continue;
        }

        let sync_id = Uuid::new_v4().to_string();
        let content_owned = content.to_string();
        let category = options.category.clone();
        let source = options.source.clone();
        let user_id = options.user_id;
        let space_id = options.space_id;
        let episode_id = options.episode_id;
        let chunk_index = chunk.index;

        match db
            .write(move |conn| {
                conn.execute(
                    "INSERT INTO memories (content, category, source, importance, user_id, space_id, \
                     episode_id, sync_id, confidence, created_at, updated_at) \
                     VALUES (?1, ?2, ?3, 5, ?4, ?5, ?6, ?7, 1.0, datetime('now'), datetime('now'))",
                    rusqlite::params![
                        content_owned,
                        category,
                        source,
                        user_id,
                        space_id,
                        episode_id,
                        sync_id
                    ],
                )
                .map_err(rusqlite_to_eng_error)?;
                Ok(())
            })
            .await
        {
            Ok(_) => {
                memories_created += 1;
                // TODO: Compute embedding and write to vec table
                // TODO: SimHash deduplication check
                // TODO: Enqueue post_store job for FSRS init, entity linking, etc.
            }
            Err(e) => {
                errors.push(format!("Chunk {}: insert failed: {}", chunk_index, e));
            }
        }
    }

    ProcessResult {
        memories_created,
        errors,
    }
}
