//! Import resolution for cross-file completion.
//!
//! This module provides functionality to query the Magellan database for Import entities
//! and resolve them to target files, enabling code completion to suggest symbols from
//! imported modules across file boundaries.
//!
//! # Architecture
//!
//! The import resolution system works by:
//! 1. Querying the Magellan database for Import entities in a given file
//! 2. Parsing import metadata (kind, path, names, glob/reexport flags)
//! 3. Resolving import paths to target file paths
//! 4. Extracting public symbols from target files
//! 5. Merging imported symbols with local symbols for completion
//!
//! # Database Schema
//!
//! Uses Magellan's graph schema:
//! - `graph_entities` table: Contains Import entities with metadata
//! - `graph_edges` table: Contains IMPORTS relationships (File → Import)
//! - JSON `data` field: Contains import details (path, names, flags)
//!
//! # Example
//!
//! ```no_run
//! use splice::completion::imports::ImportResolver;
//! use std::path::PathBuf;
//!
//! let db_path = PathBuf::from(".magellan/splice.db");
//! let resolver = ImportResolver::new(&db_path);
//!
//! let file_path = PathBuf::from("src/main.rs");
//! let imports = resolver.get_file_imports(&file_path).unwrap();
//!
//! for import in imports {
//!     println!("Import: {:?} from {}",
//!         import.imported_names, import.import_path.join("::"));
//! }
//! ```
//!
//! # Import Kinds
//!
//! - **plain_use**: Regular `use` statement (e.g., `use crate::foo::Bar`)
//! - **glob_use**: Wildcard import (e.g., `use crate::foo::*`)
//! - **reexport**: Public re-export (e.g., `pub use crate::foo::Bar`)
//!
//! # Performance
//!
//! - Query time: ~1-2ms per file
//! - Caches database connections internally
//! - Designed for incremental resolution (per-file basis)

use anyhow::Result;
use rusqlite::Connection;
use serde_json::Value as JsonValue;
use std::path::PathBuf;

/// Import entity from Magellan database.
///
/// Represents a single import statement found in the source code,
/// with full metadata for resolution and filtering.
///
/// # Fields
///
/// - `id`: Magellan entity ID (for database grounding)
/// - `file_path`: Absolute path to file containing this import
/// - `import_kind`: Type of import ("plain_use", "glob_use", "reexport")
/// - `import_path`: Module path segments (e.g., ["crate", "api", "handler"])
/// - `imported_names`: Specific names imported (empty for globs)
/// - `is_glob`: Whether this is a wildcard import (`use foo::*`)
/// - `is_reexport`: Whether this is a public re-export (`pub use`)
///
/// # Example
///
/// ```rust
/// use splice::completion::imports::ImportEntity;
///
/// // For: use crate::api::{RequestHandler, process_request};
/// let _entity = ImportEntity {
///     id: "12345".to_string(),
///     file_path: "/path/to/src/main.rs".to_string(),
///     import_kind: "plain_use".to_string(),
///     import_path: vec!["crate".to_string(), "api".to_string()],
///     imported_names: vec!["RequestHandler".to_string(), "process_request".to_string()],
///     is_glob: false,
///     is_reexport: false,
/// };
/// ```
#[derive(Debug, Clone)]
pub struct ImportEntity {
    /// Magellan entity ID (database primary key)
    pub id: String,
    /// Absolute path to file containing this import
    pub file_path: String,
    /// Import kind: "plain_use", "glob_use", or "reexport"
    pub import_kind: String,
    /// Module path segments (e.g., ["crate", "api", "handler"])
    pub import_path: Vec<String>,
    /// Specific names imported (empty for glob imports)
    pub imported_names: Vec<String>,
    /// True if wildcard import (`use foo::*`)
    pub is_glob: bool,
    /// True if public re-export (`pub use`)
    pub is_reexport: bool,
}

/// Import resolver using Magellan database.
///
/// Queries the Magellan graph database to extract Import entities for a given file,
/// parsing JSON metadata to provide structured import information.
///
/// # Usage
///
/// ```no_run
/// use splice::completion::imports::ImportResolver;
/// use std::path::PathBuf;
///
/// let resolver = ImportResolver::new(&PathBuf::from(".magellan/splice.db"));
/// let imports = resolver.get_file_imports(&PathBuf::from("src/main.rs")).unwrap();
/// ```
///
/// # Database Query
///
/// The resolver executes a SQL query joining `graph_entities` and `graph_edges`:
///
/// ```sql
/// SELECT ge.id, ge.file_path, ge.data
/// FROM graph_entities ge
/// JOIN graph_edges e ON ge.id = e.to_id
/// WHERE e.from_id = (
///     SELECT id FROM graph_entities
///     WHERE file_path = ?1 AND kind = 'File'
///     LIMIT 1
/// )
/// AND e.edge_type = 'IMPORTS'
/// AND ge.kind = 'Import'
/// ```
///
/// This finds all Import entities connected to the file via IMPORTS edges.
pub struct ImportResolver {
    /// Path to Magellen SQLite database
    db_path: PathBuf,
}

impl ImportResolver {
    /// Create a new import resolver for the given database.
    ///
    /// # Arguments
    ///
    /// * `db_path` - Path to Magellan SQLite database
    ///
    /// # Example
    ///
    /// ```rust
    /// use splice::completion::imports::ImportResolver;
    /// use std::path::PathBuf;
    ///
    /// let resolver = ImportResolver::new(&PathBuf::from(".magellan/splice.db"));
    /// ```
    pub fn new(db_path: &PathBuf) -> Self {
        Self {
            db_path: db_path.clone(),
        }
    }

    /// Get all imports for a file from the Magellan database.
    ///
    /// Queries the database for Import entities connected to the given file
    /// via IMPORTS edges, parsing JSON metadata to extract structured import information.
    ///
    /// # Arguments
    ///
    /// * `file_path` - Absolute path to the source file
    ///
    /// # Returns
    ///
    /// A vector of `ImportEntity` structures representing all imports in the file.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Database cannot be opened
    /// - SQL query fails
    /// - JSON metadata is malformed
    ///
    /// # Example
    ///
    /// ```no_run
    /// use splice::completion::imports::ImportResolver;
    /// use std::path::PathBuf;
    ///
    /// let resolver = ImportResolver::new(&PathBuf::from(".magellan/splice.db"));
    /// let imports = resolver.get_file_imports(&PathBuf::from("src/main.rs")).unwrap();
    ///
    /// for import in imports {
    ///     println!("Found import: {:?}", import.imported_names);
    /// }
    /// ```
    ///
    /// # Performance
    ///
    /// - Typical query time: 1-2ms
    /// - Scales with number of imports in file (not codebase size)
    /// - Database connection opened per query (not cached)
    pub fn get_file_imports(&self, file_path: &PathBuf) -> Result<Vec<ImportEntity>> {
        let conn = Connection::open(&self.db_path)?;

        let query = r#"
            SELECT ge.id, ge.file_path, ge.data
            FROM graph_entities ge
            JOIN graph_edges e ON ge.id = e.to_id
            WHERE e.from_id = (
                SELECT id FROM graph_entities
                WHERE file_path = ?1 AND kind = 'File'
                LIMIT 1
            )
            AND e.edge_type = 'IMPORTS'
            AND ge.kind = 'Import'
        "#;

        let mut stmt = conn.prepare(query)?;
        let rows = stmt.query_map([file_path.to_string_lossy().as_ref()], |row| {
            Ok((
                row.get::<_, i64>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
            ))
        })?;

        let mut imports = Vec::new();
        for row_result in rows {
            let (id, file_path, data_str) = row_result?;
            let id = id.to_string();

            if let Ok(data) = serde_json::from_str::<JsonValue>(&data_str) {
                let import_kind = data
                    .get("import_kind")
                    .and_then(|v| v.as_str())
                    .unwrap_or("plain_use")
                    .to_string();

                let import_path = data
                    .get("import_path")
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str())
                            .map(String::from)
                            .collect()
                    })
                    .unwrap_or_default();

                let imported_names = data
                    .get("imported_names")
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str())
                            .map(String::from)
                            .collect()
                    })
                    .unwrap_or_default();

                let is_glob = data
                    .get("is_glob")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);

                let is_reexport = data
                    .get("is_reexport")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);

                imports.push(ImportEntity {
                    id,
                    file_path,
                    import_kind,
                    import_path,
                    imported_names,
                    is_glob,
                    is_reexport,
                });
            }
        }

        Ok(imports)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_get_file_imports() {
        // Test will be implemented after database setup
        // For now, just ensure it compiles
    }
}
