use anyhow::Result;
use std::path::Path;
use tree_sitter::Node;

use crate::analyzers::ast_analyzer::AstAnalyzer;
use crate::types::{Severity, Vulnerability, VulnerabilityType};

use super::LanguageAnalyzer;

pub struct RubyAnalyzer;

impl RubyAnalyzer {
    pub fn new() -> Self {
        Self
    }

    fn check_ruby_import(
        &self,
        node_text: &str,
        node: &Node,
        file_path: &Path,
        source: &str,
        analyzer: &AstAnalyzer,
    ) -> Option<Vulnerability> {
        let lower = node_text.to_lowercase();
        
        // OpenSSL library
        if lower.contains("openssl") {
            return Some(analyzer.create_vulnerability(
                node,
                file_path,
                VulnerabilityType::Rsa2048,
                Severity::Medium,
                "Ruby OpenSSL library import detected - verify usage is quantum-safe",
                "Audit usage for RSA/EC/DSA",
                source,
            ));
        }
        
        None
    }
}

impl LanguageAnalyzer for RubyAnalyzer {
    fn check_node(
        &self,
        node: &Node,
        source: &str,
        file_path: &Path,
        analyzer: &AstAnalyzer,
    ) -> Option<Vulnerability> {
        let node_type = node.kind();
        let node_text = match node.utf8_text(source.as_bytes()) {
            Ok(text) => text,
            Err(_) => return None,
        };

        match node_type {
            "call" => {
                let lower = node_text.to_lowercase();

                // Ruby OpenSSL library (RSA/EC/DH)
                if lower.contains("openssl::pkey::rsa") || lower.contains("openssl::pkey::ec") || lower.contains("openssl::pkey::dsa") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::Rsa2048,
                        Severity::Critical,
                        "Ruby OpenSSL RSA/EC/DSA usage detected",
                        "Migrate to post-quantum cryptography",
                        source,
                    ));
                }

                // Weak Hashes
                if lower.contains("digest::md5") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::Md5,
                        Severity::High,
                        "Ruby MD5 hash function detected",
                        "Replace with SHA-256 or SHA-3",
                        source,
                    ));
                }
                if lower.contains("digest::sha1") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::Sha1,
                        Severity::High,
                        "Ruby SHA1 hash function detected",
                        "Replace with SHA-256 or SHA-3",
                        source,
                    ));
                }

                // Weak Random
                if lower.contains("rand(") && !lower.contains("securerandom") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::WeakRandom,
                        Severity::Medium,
                        "Ruby non-cryptographic RNG detected (rand)",
                        "Use SecureRandom for cryptographic operations",
                        source,
                    ));
                }

                None
            }
            "require" | "module_declaration" => {
                self.check_ruby_import(node_text, node, file_path, source, analyzer)
            }
            "string" => {
                analyzer.check_string_literal(node_text, node, file_path, source, node)
            }
            "integer" => {
                analyzer.check_key_size_literal(node_text, node, file_path, source)
            }
            _ => None,
        }
    }
}
