//! DuckDB-backed primitive stores: thread-safe connection pool plus two
//! tables — `blobs` (raw bytes per UUID/string key) and `json_docs`
//! (one JSON document per UUID with a logical key field for secondary
//! lookups).
//!
//! This module is the absorbed equivalent of bdslib's
//! `storageengine.rs` + `datastorage.rs`. The differences from the
//! original:
//!
//! - Returns `anyhow::Result` instead of `easy_error::Result`.
//! - No `rust_dynamic::value::Value` bridge; we pull `Vec<u8>` /
//!   `String` directly out of `duckdb::types::Value`.
//! - No shared `ScheduledThreadPool` — r2d2's per-pool default is
//!   fine for inkhaven's one-process / one-store usage.
//! - String-key blob helpers are gone (never reached from inkhaven).
//! - `JsonStorageConfig.key_field` is gone too (we always use the
//!   default key — that's the only mode inkhaven ever invoked).

use anyhow::{anyhow, Result};
use duckdb::{types::Value as DuckValue, DuckdbConnectionManager};
use r2d2::Pool;
use serde_json::Value as JsonValue;
use std::path::Path;
use std::sync::Arc;
use uuid::Uuid;

// ── StorageEngine ────────────────────────────────────────────────────

/// Thin wrapper over an r2d2-pooled DuckDB connection. Cloneable; all
/// clones share the same pool.
pub struct StorageEngine {
    pool: Pool<DuckdbConnectionManager>,
}

impl StorageEngine {
    /// Open or create the DuckDB database at `path`, run `init_sql` once
    /// to set up the schema, and return a connection pool of `pool_size`
    /// connections.
    pub fn new<P: AsRef<Path>>(path: P, init_sql: &str, pool_size: u32) -> Result<Self> {
        let manager = DuckdbConnectionManager::file(path)
            .map_err(|e| anyhow!("failed to create connection manager: {e}"))?;

        let pool = Pool::builder()
            .max_size(pool_size)
            .build(manager)
            .map_err(|e| anyhow!("failed to initialize connection pool: {e}"))?;

        {
            let conn = pool
                .get()
                .map_err(|e| anyhow!("could not get init connection: {e}"))?;
            conn.execute_batch(init_sql)
                .map_err(|e| anyhow!("initialization SQL failed: {e}"))?;
        }

        Ok(Self { pool })
    }

    /// Run a `SELECT` and collect every row as a `Vec` of raw DuckDB
    /// values. Used for the tiny set of internal queries this module
    /// emits — callers `match` on the variants directly.
    pub fn select_all(&self, sql: &str) -> Result<Vec<Vec<DuckValue>>> {
        let conn = self
            .pool
            .get()
            .map_err(|e| anyhow!("pool checkout failed: {e}"))?;

        let mut stmt = conn
            .prepare(sql)
            .map_err(|e| anyhow!("query preparation failed: {e}"))?;

        // duckdb-rs panics if `column_count` is read before the
        // statement has been executed, so query first and read the
        // column count off each row as it arrives.
        let rows = stmt
            .query_map([], |row| {
                let n = row.as_ref().column_count();
                let mut out = Vec::with_capacity(n);
                for i in 0..n {
                    out.push(row.get::<_, DuckValue>(i)?);
                }
                Ok(out)
            })
            .map_err(|e| anyhow!("execution of select_all failed: {e}"))?;

        let mut results = Vec::new();
        for r in rows {
            results.push(r.map_err(|e| anyhow!("error fetching row: {e}"))?);
        }
        Ok(results)
    }

    /// Same as [`select_all`] but with positional parameters — for
    /// SELECTs that filter on an id/key value, so it binds rather than
    /// string-interpolating (no escaping, no injection surface).
    pub fn select_all_with(
        &self,
        sql: &str,
        args: &[&dyn duckdb::ToSql],
    ) -> Result<Vec<Vec<DuckValue>>> {
        let conn = self
            .pool
            .get()
            .map_err(|e| anyhow!("pool checkout failed: {e}"))?;
        let mut stmt = conn
            .prepare(sql)
            .map_err(|e| anyhow!("query preparation failed: {e}"))?;
        let rows = stmt
            .query_map(duckdb::params_from_iter(args.iter().copied()), |row| {
                let n = row.as_ref().column_count();
                let mut out = Vec::with_capacity(n);
                for i in 0..n {
                    out.push(row.get::<_, DuckValue>(i)?);
                }
                Ok(out)
            })
            .map_err(|e| anyhow!("execution of select_all_with failed: {e}"))?;
        let mut results = Vec::new();
        for r in rows {
            results.push(r.map_err(|e| anyhow!("error fetching row: {e}"))?);
        }
        Ok(results)
    }

    /// Execute a DML statement (no result rows).
    pub fn execute(&self, sql: &str) -> Result<()> {
        let conn = self
            .pool
            .get()
            .map_err(|e| anyhow!("pool checkout failed: {e}"))?;
        conn.execute(sql, [])
            .map_err(|e| anyhow!("SQL execution failed: {e}"))?;
        Ok(())
    }

    /// Same as [`execute`] but with positional parameters. We use this
    /// for any statement that interpolates user-controlled bytes
    /// (BLOB writes), so the bytes never need hex encoding.
    pub fn execute_with(&self, sql: &str, args: &[&dyn duckdb::ToSql]) -> Result<()> {
        let conn = self
            .pool
            .get()
            .map_err(|e| anyhow!("pool checkout failed: {e}"))?;
        conn.execute(sql, duckdb::params_from_iter(args.iter().copied()))
            .map_err(|e| anyhow!("SQL execution failed: {e}"))?;
        Ok(())
    }

    /// Force a DuckDB `CHECKPOINT` — drains WAL into the main `.db`
    /// file. Cheap when the WAL is empty (DuckDB short-circuits).
    /// Called from the background sync tick and the TUI shutdown
    /// path; per-save callers shouldn't invoke this directly because
    /// every commit is already durable.
    pub fn checkpoint(&self) -> Result<()> {
        self.execute("CHECKPOINT;")
    }

    /// 1.2.16+ Phase P.4 — DuckDB integrity check.
    /// Runs `PRAGMA integrity_check` and returns
    /// the result string.  On a clean database
    /// DuckDB returns `"ok"`; on corruption it
    /// returns a list of issues — non-`"ok"` is
    /// the failure signal.
    ///
    /// Cheap on healthy databases (single-digit
    /// milliseconds for typical project sizes);
    /// scales linearly with corruption.  The
    /// health monitor's 15 min cadence accounts
    /// for that.
    pub fn integrity_check(&self) -> Result<String> {
        let rows = self.select_all("PRAGMA integrity_check;")?;
        // Expected shape: one row, one column, text
        // `"ok"` on healthy.  Older DuckDB versions
        // returned empty result instead of `"ok"`
        // — treat empty as healthy too.
        let val = rows.first().and_then(|r| r.first());
        match val {
            Some(duckdb::types::Value::Text(s)) => Ok(s.clone()),
            Some(other) => Ok(format!("{other:?}")),
            None => Ok("ok".to_string()),
        }
    }
}

// ── BlobStorage ──────────────────────────────────────────────────────

const BLOB_INIT_SQL: &str = "
    CREATE TABLE IF NOT EXISTS blobs (
        id         TEXT   NOT NULL PRIMARY KEY,
        created_at BIGINT NOT NULL,
        updated_at BIGINT NOT NULL,
        data       BLOB   NOT NULL
    );
";

/// Binary blob store keyed by UUID. Thread-safe (cloning yields a new
/// handle to the same pool).
#[derive(Clone)]
pub struct BlobStorage {
    engine: Arc<StorageEngine>,
}

impl BlobStorage {
    pub fn new<P: AsRef<Path>>(path: P, pool_size: u32) -> Result<Self> {
        let engine = StorageEngine::new(path, BLOB_INIT_SQL, pool_size)?;
        Ok(Self {
            engine: Arc::new(engine),
        })
    }

    /// Insert a new blob keyed by `id`. Errors if `id` already exists.
    pub fn add_blob_with_key(&self, id: Uuid, data: &[u8]) -> Result<()> {
        let ts = now_unix_secs();
        let id_str = id.to_string();
        self.engine.execute_with(
            "INSERT INTO blobs (id, created_at, updated_at, data) \
             VALUES (?, ?, ?, ?)",
            &[&id_str, &ts, &ts, &data],
        )
    }

    pub fn get_blob(&self, id: Uuid) -> Result<Option<Vec<u8>>> {
        let id_str = id.to_string();
        let rows = self
            .engine
            .select_all_with("SELECT data FROM blobs WHERE id = ?", &[&id_str])?;
        match rows.into_iter().next() {
            None => Ok(None),
            Some(row) => {
                let v = row
                    .into_iter()
                    .next()
                    .ok_or_else(|| anyhow!("blob row missing data column"))?;
                match v {
                    DuckValue::Blob(b) => Ok(Some(b)),
                    DuckValue::Null => Ok(None),
                    other => Err(anyhow!(
                        "unexpected blob column type: {other:?}"
                    )),
                }
            }
        }
    }

    pub fn update_blob(&self, id: Uuid, data: &[u8]) -> Result<()> {
        let ts = now_unix_secs();
        let id_str = id.to_string();
        self.engine.execute_with(
            "UPDATE blobs SET data = ?, updated_at = ? WHERE id = ?",
            &[&data, &ts, &id_str],
        )
    }

    pub fn drop_blob(&self, id: Uuid) -> Result<()> {
        let id_str = id.to_string();
        self.engine
            .execute_with("DELETE FROM blobs WHERE id = ?", &[&id_str])
    }

    pub fn checkpoint(&self) -> Result<()> {
        self.engine.checkpoint()
    }

    /// 1.2.16+ Phase P.4 — DuckDB integrity check
    /// delegated to the underlying engine.
    pub fn integrity_check(&self) -> Result<String> {
        self.engine.integrity_check()
    }
}

// ── JsonStorage ──────────────────────────────────────────────────────

const JSON_INIT_SQL: &str = "
    CREATE TABLE IF NOT EXISTS json_docs (
        id         TEXT   NOT NULL PRIMARY KEY,
        created_at BIGINT NOT NULL,
        updated_at BIGINT NOT NULL,
        key        TEXT   NOT NULL,
        document   JSON   NOT NULL
    );
    CREATE INDEX IF NOT EXISTS idx_json_docs_key ON json_docs (key);
";

/// JSON document store keyed by UUID, with a `key` column used by
/// bdslib for secondary lookups. Inkhaven always uses the same key
/// (`"doc"`) for every record — the field is preserved purely for
/// on-disk compatibility with existing projects.
#[derive(Clone)]
pub struct JsonStorage {
    engine: Arc<StorageEngine>,
    default_key: Arc<String>,
}

impl JsonStorage {
    pub fn new<P: AsRef<Path>>(path: P, pool_size: u32, default_key: &str) -> Result<Self> {
        let engine = StorageEngine::new(path, JSON_INIT_SQL, pool_size)?;
        Ok(Self {
            engine: Arc::new(engine),
            default_key: Arc::new(default_key.to_string()),
        })
    }

    pub fn add_json_with_id(&self, id: Uuid, document: JsonValue) -> Result<()> {
        let ts = now_unix_secs();
        let id_str = id.to_string();
        let doc_str = serde_json::to_string(&document)
            .map_err(|e| anyhow!("JSON serialisation failed: {e}"))?;
        let key: &str = &self.default_key;
        self.engine.execute_with(
            "INSERT INTO json_docs (id, created_at, updated_at, key, document) \
             VALUES (?, ?, ?, ?, CAST(? AS JSON))",
            &[&id_str, &ts, &ts, &key, &doc_str],
        )
    }

    pub fn get_json(&self, id: Uuid) -> Result<Option<JsonValue>> {
        let id_str = id.to_string();
        let rows = self.engine.select_all_with(
            "SELECT document FROM json_docs WHERE id = ?",
            &[&id_str],
        )?;
        match rows.into_iter().next() {
            None => Ok(None),
            Some(row) => {
                let v = row
                    .into_iter()
                    .next()
                    .ok_or_else(|| anyhow!("json_docs row missing document column"))?;
                let text = match v {
                    DuckValue::Text(s) => s,
                    DuckValue::Null => return Ok(None),
                    other => {
                        return Err(anyhow!(
                            "unexpected json document column type: {other:?}"
                        ))
                    }
                };
                let val = serde_json::from_str(&text)
                    .map_err(|e| anyhow!("JSON parse failed: {e}"))?;
                Ok(Some(val))
            }
        }
    }

    pub fn update_json(&self, id: Uuid, document: JsonValue) -> Result<()> {
        let ts = now_unix_secs();
        let id_str = id.to_string();
        let doc_str = serde_json::to_string(&document)
            .map_err(|e| anyhow!("JSON serialisation failed: {e}"))?;
        let key: &str = &self.default_key;
        self.engine.execute_with(
            "UPDATE json_docs \
             SET document = CAST(? AS JSON), key = ?, updated_at = ? \
             WHERE id = ?",
            &[&doc_str, &key, &ts, &id_str],
        )
    }

    pub fn drop_json(&self, id: Uuid) -> Result<()> {
        let id_str = id.to_string();
        self.engine
            .execute_with("DELETE FROM json_docs WHERE id = ?", &[&id_str])
    }

    pub fn list_all(&self) -> Result<Vec<(Uuid, JsonValue)>> {
        let rows = self
            .engine
            .select_all("SELECT id, document FROM json_docs")?;
        let mut out = Vec::with_capacity(rows.len());
        for row in rows {
            let mut cols = row.into_iter();
            let id_str = match cols.next() {
                Some(DuckValue::Text(s)) => s,
                Some(other) => {
                    return Err(anyhow!(
                        "json_docs row id column unexpected type: {other:?}"
                    ))
                }
                None => return Err(anyhow!("json_docs row missing id")),
            };
            let doc_str = match cols.next() {
                Some(DuckValue::Text(s)) => s,
                Some(other) => {
                    return Err(anyhow!(
                        "json_docs row document column unexpected type: {other:?}"
                    ))
                }
                None => return Err(anyhow!("json_docs row missing document")),
            };
            let id = Uuid::parse_str(&id_str)
                .map_err(|e| anyhow!("invalid UUID in json_docs: {e}"))?;
            let doc: JsonValue = serde_json::from_str(&doc_str)
                .map_err(|e| anyhow!("JSON parse failed: {e}"))?;
            out.push((id, doc));
        }
        Ok(out)
    }

    pub fn checkpoint(&self) -> Result<()> {
        self.engine.checkpoint()
    }

    /// 1.2.16+ Phase P.4 — DuckDB integrity check
    /// delegated to the underlying engine.
    pub fn integrity_check(&self) -> Result<String> {
        self.engine.integrity_check()
    }
}

// ── helpers ──────────────────────────────────────────────────────────

fn now_unix_secs() -> i64 {
    chrono::Utc::now().timestamp()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use tempfile::TempDir;

    /// 1.2.23 SQL hardening — the JSON store binds parameters instead of
    /// string-interpolating, so content with single quotes, double
    /// quotes, and backslashes round-trips intact (the old
    /// quote-doubling `sql_escape` didn't touch backslashes).
    #[test]
    fn json_store_roundtrips_adversarial_content() {
        let dir = TempDir::new().unwrap();
        let store = JsonStorage::new(&dir.path().join("meta.db"), 2, "doc").unwrap();
        let id = Uuid::now_v7();
        let doc = json!({
            "title": "O'Brien's \"tale\" — back\\slash and a ' quote",
            "note": "'; DROP TABLE json_docs; --",
            "path": "C:\\Users\\x\\é.txt",
        });
        store.add_json_with_id(id, doc.clone()).unwrap();
        assert_eq!(store.get_json(id).unwrap(), Some(doc.clone()));

        let updated = json!({ "title": "still O'Brien's", "n": 2 });
        store.update_json(id, updated.clone()).unwrap();
        assert_eq!(store.get_json(id).unwrap(), Some(updated));

        // The table is intact (the injection-looking string was data).
        assert_eq!(store.list_all().unwrap().len(), 1);
        store.drop_json(id).unwrap();
        assert_eq!(store.get_json(id).unwrap(), None);
    }

    /// `App::shutdown_flush` calls `Store::checkpoint()`, which fans out to
    /// `JsonStorage::checkpoint()` + `BlobStorage::checkpoint()`. Verify
    /// both checkpoint paths drain a fresh write into the main DB file —
    /// i.e. no `.wal` should remain after checkpoint + drop.
    #[test]
    fn checkpoint_drains_wal_after_write() {
        let dir = TempDir::new().unwrap();

        // JSON path
        let json_path = dir.path().join("meta.db");
        {
            let store = JsonStorage::new(&json_path, 2, "doc").unwrap();
            let id = Uuid::now_v7();
            store
                .add_json_with_id(id, json!({"hello": "world"}))
                .unwrap();
            store.checkpoint().unwrap();
        }
        assert!(json_path.exists(), "main JSON file should exist");
        let wal = json_path.with_extension("db.wal");
        assert!(
            !wal.exists() || std::fs::metadata(&wal).unwrap().len() == 0,
            "WAL should be drained after checkpoint, found {} bytes",
            std::fs::metadata(&wal).map(|m| m.len()).unwrap_or(0)
        );

        // Blob path
        let blob_path = dir.path().join("blobs.db");
        {
            let store = BlobStorage::new(&blob_path, 2).unwrap();
            let id = Uuid::now_v7();
            store.add_blob_with_key(id, b"some bytes").unwrap();
            store.checkpoint().unwrap();
        }
        assert!(blob_path.exists());
        let wal = blob_path.with_extension("db.wal");
        assert!(
            !wal.exists() || std::fs::metadata(&wal).unwrap().len() == 0,
            "blob WAL should be drained after checkpoint",
        );
    }

    /// `App::shutdown_flush` doesn't error when nothing has been written —
    /// CHECKPOINT against an empty WAL is a no-op in DuckDB, but we want
    /// to be sure our wrapper doesn't choke.
    #[test]
    fn checkpoint_is_safe_on_empty_store() {
        let dir = TempDir::new().unwrap();
        let store = JsonStorage::new(dir.path().join("empty.db"), 2, "doc").unwrap();
        store.checkpoint().unwrap();
        store.checkpoint().unwrap(); // idempotent
    }

    /// Write-then-checkpoint-then-reopen round-trip — confirms data
    /// survives the explicit checkpoint exactly like it survives the
    /// pool-drop auto-checkpoint.
    #[test]
    fn data_survives_checkpoint_and_reopen() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("rt.db");
        let id = Uuid::now_v7();

        {
            let s = JsonStorage::new(&path, 2, "doc").unwrap();
            s.add_json_with_id(id, json!({"key": "value-1"})).unwrap();
            s.checkpoint().unwrap();
        }
        let s2 = JsonStorage::new(&path, 2, "doc").unwrap();
        let got = s2.get_json(id).unwrap();
        assert_eq!(got, Some(json!({"key": "value-1"})));
    }
}
