/// PHP built-in stubs — registered into the salsa db before user-code analysis.
///
/// Stubs are embedded at compile time from the `stubs/{ext}/` workspace directory via
/// `build.rs`.  Each file gets a stable virtual path (e.g. `"stubs/standard/standard_9.php"`)
/// so symbols are source-attributed and go-to-definition works for them.
///
/// # `StubVfs`
///
/// [`StubVfs`] maps every embedded stub file path to its `&'static str` content.
/// Consumers use it to serve stub file content for go-to-definition on
/// built-in PHP symbols:
///
/// ```ignore
/// let file = db.symbol_defining_file("strlen"); // → "stubs/standard/standard_0.php"
/// let src  = stub_vfs.get(&file).unwrap();           // → &'static str PHP source
/// ```
use std::collections::{HashMap, HashSet};
use std::ops::ControlFlow;
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock};

use mir_codebase::storage::StubSlice;
use php_ast::ast::ExprKind;
use php_ast::visitor::{walk_expr, walk_program, Visitor};
use php_lexer::TokenKind;
use rayon::prelude::*;

use crate::db::MirDb;
use crate::php_version::PhpVersion;

// Generated by build.rs: `static STUB_FILES: &[(&str, &str)]`
include!(concat!(env!("OUT_DIR"), "/stub_files.rs"));

// Generated by build.rs: `BUILTIN_FN_NAMES`, `STUB_FN_INDEX`,
// `STUB_CLASS_INDEX`, `STUB_CONST_INDEX`. See build.rs for details.
include!(concat!(env!("OUT_DIR"), "/phpstorm_builtin_fns.rs"));

/// Hand-curated list of stub file paths that virtually all PHP code depends
/// on. Loaded eagerly by [`crate::AnalysisSession::ensure_essential_stubs_loaded`];
/// other extension stubs are loaded on demand when user code references symbols
/// that resolve into them.
///
/// Coverage: `Core` (Throwable, ArrayAccess, …), `standard` (str_*/array_*/…),
/// `SPL` (Iterator, ArrayObject, …), `date` (DateTime). 25 of 120 stub files —
/// roughly an 80% reduction in cold-start work.
pub(crate) static ESSENTIAL_STUB_PATHS: &[&str] = &[
    "stubs/Core/Core.php",
    "stubs/Core/Core_c.php",
    "stubs/Core/Core_d.php",
    "stubs/SPL/SPL.php",
    "stubs/SPL/SPL_c1.php",
    "stubs/SPL/SPL_f.php",
    "stubs/date/date.php",
    "stubs/date/date_c.php",
    "stubs/date/date_d.php",
    "stubs/standard/_standard_manual.php",
    "stubs/standard/_types.php",
    "stubs/standard/basic.php",
    "stubs/standard/password.php",
    "stubs/standard/standard_0.php",
    "stubs/standard/standard_1.php",
    "stubs/standard/standard_10.php",
    "stubs/standard/standard_2.php",
    "stubs/standard/standard_3.php",
    "stubs/standard/standard_4.php",
    "stubs/standard/standard_5.php",
    "stubs/standard/standard_6.php",
    "stubs/standard/standard_7.php",
    "stubs/standard/standard_8.php",
    "stubs/standard/standard_9.php",
    "stubs/standard/standard_defines.php",
];

/// Look up the embedded stub file content for a virtual path (e.g.
/// `"stubs/standard/standard_0.php"`). Returns `None` if the path isn't part
/// of the embedded set.
pub(crate) fn stub_content_for_path(path: &str) -> Option<&'static str> {
    STUB_FILES
        .iter()
        .find_map(|&(p, c)| if p == path { Some(c) } else { None })
}

/// Look up the stub virtual path that defines a built-in PHP function.
/// Lookup is case-insensitive (PHP function names are case-insensitive).
/// Returns `None` if the function isn't a known built-in.
pub(crate) fn stub_path_for_function(name: &str) -> Option<&'static str> {
    let lower = name.to_ascii_lowercase();
    STUB_FN_INDEX
        .binary_search_by(|(k, _)| (*k).cmp(lower.as_str()))
        .ok()
        .map(|i| STUB_FN_INDEX[i].1)
}

/// Look up the stub virtual path that defines a built-in PHP class / interface
/// / trait / enum. Lookup is case-insensitive. Strips a single leading
/// backslash if present. Returns `None` if not a known built-in type.
pub(crate) fn stub_path_for_class(fqcn: &str) -> Option<&'static str> {
    let trimmed = fqcn.strip_prefix('\\').unwrap_or(fqcn);
    let lower = trimmed.to_ascii_lowercase();
    STUB_CLASS_INDEX
        .binary_search_by(|(k, _)| (*k).cmp(lower.as_str()))
        .ok()
        .map(|i| STUB_CLASS_INDEX[i].1)
}

/// Look up the stub virtual path that defines a built-in PHP constant.
/// PHP constants are case-sensitive, so this lookup is exact.
pub(crate) fn stub_path_for_constant(name: &str) -> Option<&'static str> {
    let trimmed = name.strip_prefix('\\').unwrap_or(name);
    STUB_CONST_INDEX
        .binary_search_by(|(k, _)| (*k).cmp(trimmed))
        .ok()
        .map(|i| STUB_CONST_INDEX[i].1)
}

/// Scan PHP `source` for identifiers that match known built-in functions /
/// classes / constants, and return the deduplicated list of stub virtual
/// paths needed to cover them.
///
/// This is the core of the lazy-stub auto-discovery used by
/// [`crate::AnalysisSession::ensure_stubs_for_source`]. Uses the PHP lexer
/// to safely extract identifier tokens, avoiding manual byte-level scanning.
/// False positives (e.g., `imagecreate` appearing inside a string literal)
/// cost only one extra stub ingest — cheap and idempotent.
pub(crate) fn collect_referenced_builtin_paths(source: &str) -> Vec<&'static str> {
    use php_lexer::lex_all;

    let mut tokens: HashSet<&str> = HashSet::new();
    let (lexed, _errors) = lex_all(source);

    let mut i = 0;
    while i < lexed.len() {
        let token = &lexed[i];
        if token.kind == TokenKind::Identifier {
            let start = token.span.start as usize;
            let end = token.span.end as usize;
            if let Some(mut text) = source.get(start..end) {
                // Handle namespaced identifiers: Foo\Bar\Baz
                let mut j = i + 1;
                while j + 1 < lexed.len()
                    && lexed[j].kind == TokenKind::Backslash
                    && lexed[j + 1].kind == TokenKind::Identifier
                {
                    j += 2;
                    if let Some(part) = source.get(start..(lexed[j - 1].span.end as usize)) {
                        text = part;
                    }
                }
                tokens.insert(text);
                i = j;
            } else {
                i += 1;
            }
        } else {
            i += 1;
        }
    }

    let mut paths: HashSet<&'static str> = HashSet::new();
    for token in tokens {
        if let Some(p) = stub_path_for_function(token) {
            paths.insert(p);
        }
        if let Some(p) = stub_path_for_class(token) {
            paths.insert(p);
        }
        if let Some(p) = stub_path_for_constant(token) {
            paths.insert(p);
        }
    }
    paths.into_iter().collect()
}

/// Walk the parsed AST to find identifiers that match known built-in functions /
/// classes / constants, and return the deduplicated list of stub virtual paths.
///
/// Unlike [`collect_referenced_builtin_paths`], this walks the real AST instead
/// of doing a byte-level heuristic scan. This eliminates false positives from
/// function names appearing inside string literals or comments. Used by
/// [`crate::AnalysisSession::ensure_stubs_for_ast`] for single-file analysis
/// where the AST is already available.
pub(crate) fn collect_referenced_builtin_paths_from_ast(
    program: &php_ast::ast::Program<'_, '_>,
) -> Vec<&'static str> {
    let mut visitor = BuiltinRefVisitor {
        paths: HashSet::new(),
    };
    let _ = walk_program(&mut visitor, program);
    visitor.paths.into_iter().collect()
}

/// Visitor for extracting function/class/constant references from the AST.
struct BuiltinRefVisitor {
    paths: HashSet<&'static str>,
}

impl<'arena, 'src> Visitor<'arena, 'src> for BuiltinRefVisitor {
    fn visit_expr(&mut self, expr: &php_ast::ast::Expr<'arena, 'src>) -> ControlFlow<()> {
        match &expr.kind {
            ExprKind::FunctionCall(call) => {
                if let ExprKind::Identifier(name) = &call.name.kind {
                    if let Some(p) = stub_path_for_function(name.as_str()) {
                        self.paths.insert(p);
                    }
                }
            }
            ExprKind::New(new_expr) => {
                if let ExprKind::Identifier(name) = &new_expr.class.kind {
                    if let Some(p) = stub_path_for_class(name.as_str()) {
                        self.paths.insert(p);
                    }
                }
            }
            ExprKind::StaticMethodCall(call) => {
                if let ExprKind::Identifier(name) = &call.class.kind {
                    if let Some(p) = stub_path_for_class(name.as_str()) {
                        self.paths.insert(p);
                    }
                }
            }
            ExprKind::ClassConstAccess(access) => {
                if let ExprKind::Identifier(name) = &access.class.kind {
                    if let Some(p) = stub_path_for_class(name.as_str()) {
                        self.paths.insert(p);
                    }
                }
            }
            ExprKind::Identifier(name) => {
                if let Some(p) = stub_path_for_constant(name.as_str()) {
                    self.paths.insert(p);
                }
            }
            _ => {}
        }
        walk_expr(self, expr)
    }
}

// ---------------------------------------------------------------------------
// Public accessors for embedded stub data
// ---------------------------------------------------------------------------

/// Every PHP stub file from `stubs/{ext}/` embedded at compile time as `(path, content)`.
///
/// Path is workspace-relative (e.g. `"stubs/standard/standard_9.php"`).
pub fn stub_files() -> &'static [(&'static str, &'static str)] {
    STUB_FILES
}

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Default-version entry point retained for callers (CLI, benches, tests) that
/// don't track a target PHP version. Equivalent to
/// [`load_stubs_for_version`] with `PhpVersion::LATEST`.
#[allow(dead_code)]
pub(crate) fn load_stubs(db: &mut MirDb) {
    load_stubs_for_version(db, PhpVersion::LATEST);
}

/// Load stubs filtered for `php_version`. Symbols whose `@since` post-dates
/// the target, or whose `@removed` is at or before the target, are skipped —
/// so multiple declarations of the same name (e.g. `each` on PHP 7 vs.
/// PHP 8) gated by `@since`/`@removed` collapse to the one matching variant.
#[allow(dead_code)]
pub(crate) fn load_stubs_for_version(db: &mut MirDb, php_version: PhpVersion) {
    for slice in builtin_stub_slices_for_version(php_version) {
        db.ingest_stub_slice(&slice);
    }
}

pub(crate) fn builtin_stub_slices_for_version(php_version: PhpVersion) -> Vec<StubSlice> {
    STUB_FILES
        .par_iter()
        .map(|(filename, content)| stub_slice_from_source(filename, content, Some(php_version)))
        .collect()
}

pub(crate) fn user_stub_slices(files: &[PathBuf], dirs: &[PathBuf]) -> Vec<StubSlice> {
    let mut all_paths: Vec<PathBuf> = files.to_vec();
    for dir in dirs {
        collect_stub_dir_paths(dir, &mut all_paths);
    }

    all_paths
        .par_iter()
        .filter_map(|path| parse_stub_file_slice(path))
        .collect()
}

pub(crate) fn stub_slice_from_source(
    filename: &str,
    content: &str,
    php_version: Option<PhpVersion>,
) -> StubSlice {
    let arena = crate::arena::create_parse_arena(content.len());
    let result = php_rs_parser::parse(&arena, content);
    let file: Arc<str> = Arc::from(filename);
    let collector =
        crate::collector::DefinitionCollector::new_for_slice(file, content, &result.source_map);
    let collector = match php_version {
        Some(version) => collector.with_php_version(version),
        None => collector,
    };
    let (mut slice, _) = collector.collect_slice(&result.program);
    mir_codebase::storage::deduplicate_params_in_slice(&mut slice);
    slice
}

fn parse_stub_file_slice(path: &Path) -> Option<StubSlice> {
    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("mir: cannot read stub file {}: {}", path.display(), e);
            return None;
        }
    };
    Some(stub_slice_from_source(
        path.to_string_lossy().as_ref(),
        &content,
        None,
    ))
}

fn collect_stub_dir_paths(dir: &Path, paths: &mut Vec<PathBuf>) {
    let entries = match std::fs::read_dir(dir) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("mir: cannot read stub directory {}: {}", dir.display(), e);
            return;
        }
    };
    let mut dir_entries: Vec<PathBuf> = entries.filter_map(|e| e.ok().map(|e| e.path())).collect();
    dir_entries.sort_unstable();
    for path in dir_entries {
        if path.is_dir() {
            collect_stub_dir_paths(&path, paths);
        } else if path.extension().is_some_and(|e| e == "php") {
            paths.push(path);
        }
    }
}

/// Parse user-provided stub files and directories into `codebase`.
///
/// Called after built-in stubs are loaded so user definitions can override or
/// supplement built-ins (e.g. framework-specific classes, IDE helpers).
#[allow(dead_code)]
pub(crate) fn load_user_stubs(db: &mut MirDb, files: &[PathBuf], dirs: &[PathBuf]) {
    for slice in user_stub_slices(files, dirs) {
        db.ingest_stub_slice(&slice);
    }
}

// ---------------------------------------------------------------------------
// StubVfs — virtual file system for stub content
// ---------------------------------------------------------------------------

/// A read-only map from stub file path to its embedded PHP source content.
///
/// Consumers use this to serve stub source for go-to-definition on built-in
/// PHP symbols.
///
/// # Example
///
/// ```ignore
/// let vfs = StubVfs::new();
/// // After analysis:
/// if let Some(path) = codebase.symbol_to_file.get("strlen") {
///     if let Some(src) = vfs.get(&path) {
///         // serve `src` as a read-only virtual document
///     }
/// }
/// ```
pub struct StubVfs {
    files: HashMap<&'static str, &'static str>,
}

impl StubVfs {
    /// Build the VFS from the embedded stub set.
    pub fn new() -> Self {
        let files = STUB_FILES.iter().map(|&(p, c)| (p, c)).collect();
        Self { files }
    }

    /// Return the PHP source for `path`, or `None` if it is not a stub file.
    pub fn get(&self, path: &str) -> Option<&'static str> {
        self.files.get(path).copied()
    }

    /// Return `true` if `path` is a known stub file path.
    pub fn is_stub_file(&self, path: &str) -> bool {
        self.files.contains_key(path)
    }
}

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

// ---------------------------------------------------------------------------
// Builtin-function query
// ---------------------------------------------------------------------------

/// Returns `true` if `name` is a known PHP built-in function.
///
/// Fast path: binary search on `BUILTIN_FN_NAMES`, a sorted compile-time slice
/// generated from `PhpStormStubsMap.php` by `build.rs`.
///
/// Fallback (when `BUILTIN_FN_NAMES` is empty): reads the embedded stub slices and checks
/// the embedded stubs and checks membership there.
///
/// # Example
/// ```
/// assert!(mir_analyzer::stubs::is_builtin_function("strlen"));
/// assert!(!mir_analyzer::stubs::is_builtin_function("my_custom_function"));
/// ```
pub fn is_builtin_function(name: &str) -> bool {
    if !BUILTIN_FN_NAMES.is_empty() {
        return BUILTIN_FN_NAMES.binary_search(&name).is_ok();
    }
    static FALLBACK: LazyLock<HashSet<Arc<str>>> = LazyLock::new(|| {
        builtin_stub_slices_for_version(PhpVersion::LATEST)
            .into_iter()
            .flat_map(|slice| slice.functions.into_iter().map(|func| func.fqn))
            .collect()
    });
    FALLBACK.contains(name)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::{
        constant_exists_via_db, function_exists_via_db, type_exists_via_db, MirDatabase, MirDb,
    };

    fn stubs_codebase() -> MirDb {
        let mut db = MirDb::default();
        load_stubs(&mut db);
        db
    }

    fn stubs_codebase_for(version: PhpVersion) -> MirDb {
        let mut db = MirDb::default();
        load_stubs_for_version(&mut db, version);
        db
    }

    fn stub_function_for(version: PhpVersion, name: &str) -> Option<mir_codebase::FunctionStorage> {
        builtin_stub_slices_for_version(version)
            .into_iter()
            .flat_map(|slice| slice.functions.into_iter())
            .find(|func| func.fqn.as_ref() == name)
    }

    fn stub_class_for(version: PhpVersion, name: &str) -> Option<mir_codebase::ClassStorage> {
        builtin_stub_slices_for_version(version)
            .into_iter()
            .flat_map(|slice| slice.classes.into_iter())
            .find(|cls| cls.fqcn.as_ref() == name)
    }

    #[test]
    fn since_tag_excludes_function_below_target() {
        // `str_contains` is `@since 8.0`.
        let cb = stubs_codebase_for(PhpVersion::new(7, 4));
        assert!(
            !function_exists_via_db(&cb, "str_contains"),
            "str_contains should not be registered on PHP 7.4"
        );
    }

    #[test]
    fn since_tag_includes_function_at_target() {
        let cb = stubs_codebase_for(PhpVersion::new(8, 0));
        assert!(
            function_exists_via_db(&cb, "str_contains"),
            "str_contains should be registered on PHP 8.0"
        );
    }

    #[test]
    fn since_filter_applies_to_classes() {
        // `\Random\Randomizer` was introduced in PHP 8.2.
        let cb_old = stubs_codebase_for(PhpVersion::new(8, 1));
        assert!(
            !type_exists_via_db(&cb_old, "Random\\Randomizer"),
            "Random\\Randomizer should not exist on PHP 8.1"
        );
        let cb_new = stubs_codebase_for(PhpVersion::new(8, 2));
        assert!(
            type_exists_via_db(&cb_new, "Random\\Randomizer"),
            "Random\\Randomizer should exist on PHP 8.2"
        );
    }

    #[test]
    fn since_filter_applies_to_methods() {
        // DateTimeImmutable::createFromInterface() was added in PHP 8.0.
        let cls = stub_class_for(PhpVersion::new(7, 4), "DateTimeImmutable")
            .expect("DateTimeImmutable must exist");
        assert!(
            !cls.own_methods.contains_key("createfrominterface"),
            "createFromInterface should be absent on PHP 7.4"
        );

        let cls_new = stub_class_for(PhpVersion::new(8, 0), "DateTimeImmutable")
            .expect("DateTimeImmutable must exist");
        assert!(
            cls_new.own_methods.contains_key("createfrominterface"),
            "createFromInterface should be present on PHP 8.0"
        );
    }

    #[test]
    fn since_tag_excludes_constant_below_target() {
        if STUB_FILES.is_empty() {
            return;
        }
        // `IMAGETYPE_AVIF` is `@since 8.1` in standard/standard_defines.php.
        let cb_old = stubs_codebase_for(PhpVersion::new(8, 0));
        assert!(
            !constant_exists_via_db(&cb_old, "IMAGETYPE_AVIF"),
            "IMAGETYPE_AVIF should not be registered on PHP 8.0"
        );
        let cb_new = stubs_codebase_for(PhpVersion::new(8, 1));
        assert!(
            constant_exists_via_db(&cb_new, "IMAGETYPE_AVIF"),
            "IMAGETYPE_AVIF should be registered on PHP 8.1"
        );
    }

    #[test]
    fn removed_tag_excludes_function_at_or_after_target() {
        // `each` is `@removed 8.0`.
        let cb = stubs_codebase_for(PhpVersion::new(8, 0));
        assert!(
            !function_exists_via_db(&cb, "each"),
            "each should be removed on PHP 8.0"
        );
        let cb74 = stubs_codebase_for(PhpVersion::new(7, 4));
        assert!(
            function_exists_via_db(&cb74, "each"),
            "each should still exist on PHP 7.4"
        );
    }

    fn assert_fn(cb: &MirDb, name: &str) {
        assert!(
            function_exists_via_db(cb, name),
            "expected stub for `{name}` to be registered"
        );
    }

    #[test]
    fn sscanf_vars_param_is_byref_and_variadic() {
        let func = stub_function_for(PhpVersion::LATEST, "sscanf").expect("sscanf must be defined");
        let vars = func.params.get(2).expect("sscanf must have a 3rd param");
        assert!(vars.is_byref, "sscanf vars param must be by-ref");
        assert!(vars.is_variadic, "sscanf vars param must be variadic");
    }

    #[test]
    fn sscanf_output_vars_not_undefined() {
        use crate::project::ProjectAnalyzer;
        use mir_issues::IssueKind;

        let src = "<?php\nfunction test($str) {\n    sscanf($str, \"%d %d\", $row, $col);\n    return $row + $col;\n}\n";
        let tmp = std::env::temp_dir().join("mir_test_sscanf_undefined.php");
        std::fs::write(&tmp, src).unwrap();
        let result = ProjectAnalyzer::new().analyze(std::slice::from_ref(&tmp));
        std::fs::remove_file(tmp).ok();
        let undef: Vec<_> = result.issues.iter()
            .filter(|i| matches!(&i.kind, IssueKind::UndefinedVariable { name } if name == "row" || name == "col"))
            .collect();
        assert!(
            undef.is_empty(),
            "sscanf output vars must not be reported as UndefinedVariable; got: {undef:?}"
        );
    }

    #[test]
    fn stream_functions_are_defined() {
        let cb = stubs_codebase();
        assert_fn(&cb, "stream_isatty");
        assert_fn(&cb, "stream_select");
        assert_fn(&cb, "stream_get_meta_data");
        assert_fn(&cb, "stream_set_blocking");
        assert_fn(&cb, "stream_copy_to_stream");
    }

    #[test]
    fn preg_grep_is_defined() {
        let cb = stubs_codebase();
        assert_fn(&cb, "preg_grep");
    }

    #[test]
    fn standard_missing_functions_are_defined() {
        let cb = stubs_codebase();
        assert_fn(&cb, "get_resource_type");
        assert_fn(&cb, "ftruncate");
        assert_fn(&cb, "umask");
        assert_fn(&cb, "date_default_timezone_set");
        assert_fn(&cb, "date_default_timezone_get");
    }

    #[test]
    fn mb_missing_functions_are_defined() {
        let cb = stubs_codebase();
        assert_fn(&cb, "mb_strwidth");
        assert_fn(&cb, "mb_convert_variables");
    }

    #[test]
    fn pcntl_functions_are_defined() {
        let cb = stubs_codebase();
        assert_fn(&cb, "pcntl_signal");
        assert_fn(&cb, "pcntl_async_signals");
        assert_fn(&cb, "pcntl_signal_get_handler");
        assert_fn(&cb, "pcntl_alarm");
    }

    #[test]
    fn posix_functions_are_defined() {
        let cb = stubs_codebase();
        assert_fn(&cb, "posix_kill");
        assert_fn(&cb, "posix_getpid");
    }

    #[test]
    fn sapi_windows_functions_are_defined() {
        let cb = stubs_codebase();
        assert_fn(&cb, "sapi_windows_vt100_support");
        assert_fn(&cb, "sapi_windows_cp_set");
        assert_fn(&cb, "sapi_windows_cp_get");
        assert_fn(&cb, "sapi_windows_cp_conv");
    }

    #[test]
    fn cli_functions_are_defined() {
        let cb = stubs_codebase();
        assert_fn(&cb, "cli_set_process_title");
        assert_fn(&cb, "cli_get_process_title");
    }

    #[test]
    fn builtin_fn_names_has_sufficient_entries() {
        // Guards against PhpStormStubsMap.php parsing regressions in build.rs.
        // When the submodule is absent the slice is empty — skip the check in that case.
        if BUILTIN_FN_NAMES.is_empty() {
            return;
        }
        assert!(
            BUILTIN_FN_NAMES.len() >= 500,
            "BUILTIN_FN_NAMES has only {} entries — \
             build.rs may have failed to parse PhpStormStubsMap.php correctly",
            BUILTIN_FN_NAMES.len()
        );
    }

    #[test]
    fn is_builtin_function_returns_true_for_known_builtins() {
        assert!(is_builtin_function("strlen"), "strlen should be a builtin");
        assert!(
            is_builtin_function("array_map"),
            "array_map should be a builtin"
        );
        assert!(
            is_builtin_function("json_encode"),
            "json_encode should be a builtin"
        );
        assert!(
            is_builtin_function("preg_match"),
            "preg_match should be a builtin"
        );
    }

    #[test]
    fn is_builtin_function_covers_stdlib_functions() {
        assert!(is_builtin_function("bcadd"), "bcadd should be a builtin");
        assert!(
            is_builtin_function("sodium_crypto_secretbox"),
            "sodium_crypto_secretbox should be a builtin"
        );
    }

    #[test]
    fn is_builtin_function_returns_false_for_unknown_names() {
        assert!(
            !is_builtin_function("my_custom_function"),
            "my_custom_function should not be a builtin"
        );
        assert!(
            !is_builtin_function(""),
            "empty string should not be a builtin"
        );
        assert!(
            !is_builtin_function("ast\\parse_file"),
            "extension function should not be a builtin"
        );
    }

    // --- stub coverage tests ---

    fn assert_cls(cb: &MirDb, name: &str) {
        assert!(
            type_exists_via_db(cb, name),
            "expected stub class `{name}` to be registered"
        );
    }

    fn assert_iface(cb: &MirDb, name: &str) {
        assert!(
            type_exists_via_db(cb, name),
            "expected stub interface `{name}` to be registered"
        );
    }

    fn assert_const(cb: &MirDb, name: &str) {
        assert!(
            constant_exists_via_db(cb, name),
            "expected stub constant `{name}` to be registered"
        );
    }

    #[test]
    fn stubs_coverage_counts() {
        let cb = stubs_codebase();
        let fn_count = cb.function_count();
        let type_count = cb.type_count();
        let const_count = cb.constant_count();
        // Sanity lower bounds — phpstorm-stubs is comprehensive.
        assert!(fn_count > 500, "expected >500 functions, got {fn_count}");
        assert!(type_count > 120, "expected >120 types, got {type_count}");
        assert!(
            const_count > 200,
            "expected >200 constants, got {const_count}"
        );
    }

    #[test]
    fn curl_multi_exec_still_running_is_byref() {
        let func = stub_function_for(PhpVersion::LATEST, "curl_multi_exec")
            .expect("curl_multi_exec must be defined");
        let still_running = func
            .params
            .iter()
            .find(|p| p.name.as_ref() == "still_running")
            .expect("curl_multi_exec must have a still_running param");
        assert!(
            still_running.is_byref,
            "curl_multi_exec $still_running must be by-ref (generated from PHP stub)"
        );
    }

    // --- Stub loading regression tests ---

    #[test]
    fn stub_files_are_non_empty() {
        // Regression: STUB_FILES was silently empty when build.rs used
        // the crate manifest dir instead of the workspace root to locate stubs/.
        assert!(
            !STUB_FILES.is_empty(),
            "STUB_FILES must not be empty — check build.rs find_workspace_root()"
        );
    }

    #[test]
    fn stub_vfs_resolves_all_paths() {
        let vfs = StubVfs::new();
        for &(path, expected_content) in STUB_FILES {
            let got = vfs
                .get(path)
                .unwrap_or_else(|| panic!("StubVfs::get({path:?}) returned None"));
            assert_eq!(got, expected_content, "StubVfs content mismatch for {path}");
            assert!(
                vfs.is_stub_file(path),
                "StubVfs::is_stub_file({path:?}) returned false"
            );
        }
    }

    #[test]
    fn stub_vfs_rejects_user_file_paths() {
        let vfs = StubVfs::new();
        assert!(!vfs.is_stub_file("/tmp/user_code.php"));
        assert!(!vfs.is_stub_file("src/MyClass.php"));
        assert!(!vfs.is_stub_file(""));
    }

    #[test]
    fn symbol_to_file_paths_are_resolvable_via_stub_vfs() {
        let mut db = MirDb::default();
        load_stubs(&mut db);
        let vfs = StubVfs::new();

        for symbol in db.active_function_node_fqns() {
            let Some(path) = db.symbol_defining_file(symbol.as_ref()) else {
                continue;
            };
            assert!(
                vfs.get(path.as_ref()).is_some(),
                "symbol '{}' points to '{}' which StubVfs cannot resolve — \
                 go-to-definition would silently break for this symbol",
                symbol,
                path
            );
        }
    }

    #[test]
    fn function_lookup_is_case_insensitive() {
        // PHP function names are case-insensitive: `STRLEN($x)` must resolve
        // to the same node as `strlen($x)`. Regression for users seeing
        // `UndefinedFunction: Function Restore_Error_Handler() is not defined`
        // on mixed-case calls of built-ins.
        let cb = stubs_codebase();
        assert!(function_exists_via_db(&cb, "strlen"));
        assert!(function_exists_via_db(&cb, "STRLEN"));
        assert!(function_exists_via_db(&cb, "StrLen"));
        assert!(function_exists_via_db(&cb, "Restore_Error_Handler"));
        assert!(function_exists_via_db(&cb, "RESTORE_ERROR_HANDLER"));
    }

    #[test]
    fn class_lookup_is_case_insensitive() {
        // PHP class names are case-insensitive: `new arrayobject()` must
        // resolve to `ArrayObject`. Regression for `UndefinedClass` on
        // lower- or upper-cased built-in class references.
        let cb = stubs_codebase();
        assert!(type_exists_via_db(&cb, "ArrayObject"));
        assert!(type_exists_via_db(&cb, "arrayobject"));
        assert!(type_exists_via_db(&cb, "ARRAYOBJECT"));
        assert!(type_exists_via_db(&cb, "ArrayOBJECT"));
    }

    #[test]
    fn constant_lookup_stays_case_sensitive() {
        // PHP global constants ARE case-sensitive (except true/false/null).
        // Make sure the case-insensitivity fix for functions/classes did not
        // leak into constants.
        let cb = stubs_codebase();
        assert!(constant_exists_via_db(&cb, "PHP_INT_MAX"));
        assert!(!constant_exists_via_db(&cb, "php_int_max"));
        assert!(!constant_exists_via_db(&cb, "Php_Int_Max"));
    }

    #[test]
    fn stdlib_symbols_are_loaded() {
        let cb = stubs_codebase();

        assert_fn(&cb, "bcadd");
        assert_fn(&cb, "bcsub");
        assert_fn(&cb, "bcmul");
        assert_fn(&cb, "bcdiv");
        assert_fn(&cb, "sodium_crypto_secretbox");
        assert_fn(&cb, "sodium_randombytes_buf");

        assert_cls(&cb, "SplObjectStorage");
        assert_cls(&cb, "SplHeap");
        assert_cls(&cb, "IteratorIterator");
        assert_cls(&cb, "FilterIterator");
        assert_cls(&cb, "LimitIterator");
        assert_cls(&cb, "CallbackFilterIterator");
        assert_cls(&cb, "RegexIterator");
        assert_cls(&cb, "AppendIterator");
        assert_cls(&cb, "GlobIterator");
        assert_cls(&cb, "ReflectionObject");
        assert_cls(&cb, "Attribute");

        assert_iface(&cb, "SeekableIterator");
        assert_iface(&cb, "SplObserver");
        assert_iface(&cb, "SplSubject");

        assert_const(&cb, "PHP_INT_MAX");
        assert_const(&cb, "PHP_INT_MIN");
        assert_const(&cb, "PHP_EOL");
        assert_const(&cb, "SORT_REGULAR");
        assert_const(&cb, "JSON_THROW_ON_ERROR");
        assert_const(&cb, "FILTER_VALIDATE_EMAIL");
        assert_const(&cb, "PREG_OFFSET_CAPTURE");
        assert_const(&cb, "M_PI");
        assert_const(&cb, "PASSWORD_DEFAULT");
    }
}
