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 CSharpAnalyzer;

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

    fn check_csharp_import(
        &self,
        node_text: &str,
        node: &Node,
        file_path: &Path,
        source: &str,
        analyzer: &AstAnalyzer,
    ) -> Option<Vulnerability> {
        let lower = node_text.to_lowercase();
        
        if lower.contains("system.security.cryptography") {
            if lower.contains("rsa") || lower.contains("ecdsa") || lower.contains("dsa") {
                return Some(analyzer.create_vulnerability(
                    node,
                    file_path,
                    VulnerabilityType::Rsa2048,
                    Severity::Medium,
                    "System.Security.Cryptography import detected - verify usage is quantum-safe",
                    "Audit usage for RSA/ECDSA/DSA and weak hashes/ciphers",
                    source,
                ));
            }
        }
        // BouncyCastle for C#
        if lower.contains("org.bouncycastle") {
            if lower.contains("rsa") || lower.contains("ec") || lower.contains("dsa") {
                return Some(analyzer.create_vulnerability(
                    node,
                    file_path,
                    VulnerabilityType::Rsa2048,
                    Severity::Medium,
                    "BouncyCastle C# import detected - verify usage is quantum-safe",
                    "Migrate to post-quantum cryptography",
                    source,
                ));
            }
        }
        
        None
    }
}

impl LanguageAnalyzer for CSharpAnalyzer {
    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 {
            "object_creation_expression" | "invocation_expression" => {
                let lower = node_text.to_lowercase();

                // RSA/ECDSA/DSA
                if lower.contains("rsa") && lower.contains("create") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::Rsa2048,
                        Severity::Critical,
                        "C# RSA object creation detected (System.Security.Cryptography)",
                        "Migrate to post-quantum key encapsulation (ML-KEM/Kyber)",
                        source,
                    ));
                }
                if lower.contains("ecdsa") && lower.contains("create") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::Ecdsa,
                        Severity::Critical,
                        "C# ECDSA object creation detected (System.Security.Cryptography)",
                        "Migrate to post-quantum signatures (ML-DSA/Dilithium)",
                        source,
                    ));
                }
                if lower.contains("dsa") && lower.contains("create") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::Dsa,
                        Severity::Critical,
                        "C# DSA object creation detected (System.Security.Cryptography)",
                        "Migrate to post-quantum signatures (ML-DSA/Dilithium)",
                        source,
                    ));
                }

                // Weak Ciphers/Hashes
                if lower.contains("tripledes") || lower.contains("des") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::TripleDes,
                        Severity::Critical,
                        "C# TripleDES/DES cipher detected",
                        "Replace with AES-256-GCM",
                        source,
                    ));
                }
                if lower.contains("md5") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::Md5,
                        Severity::High,
                        "C# MD5 hash function detected",
                        "Replace with SHA-256 or SHA-3",
                        source,
                    ));
                }
                if lower.contains("sha1") {
                    return Some(analyzer.create_vulnerability(
                        node,
                        file_path,
                        VulnerabilityType::Sha1,
                        Severity::High,
                        "C# SHA1 hash function detected",
                        "Replace with SHA-256 or SHA-3",
                        source,
                    ));
                }

                None
            }
            "using_directive" => {
                self.check_csharp_import(node_text, node, file_path, source, analyzer)
            }
            "string_literal" | "interpolated_string_expression" => {
                analyzer.check_string_literal(node_text, node, file_path, source, node)
            }
            "numeric_literal" => {
                analyzer.check_key_size_literal(node_text, node, file_path, source)
            }
            _ => None,
        }
    }
}
