use std::path::Path;

/// Redis detector
/// Detects: Redis usage via dependencies, connection strings
use super::helpers;
use crate::error::DetectionResult;
use crate::models::{Detection, Evidence, Technology};

pub struct RedisDetector;

impl Default for RedisDetector {
    fn default() -> Self {
        Self
    }
}

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

impl super::Detector for RedisDetector {
    fn name(&self) -> &str {
        "redis"
    }

    fn detect(&self, project_path: &Path) -> DetectionResult<Option<Detection>> {
        let mut confidence = 0.0;
        let mut evidence = Vec::new();

        // Check for redis dependency in package.json
        if let Ok(content) = helpers::read_file(project_path, "package.json") {
            if content.contains("\"redis\"") {
                evidence.push(Evidence::Dependency {
                    name: "redis".into(),
                    version: None,
                    reason: "Redis client for Node.js".into(),
                });
                confidence += 0.4;
            }
        }

        // Check for Redis in requirements.txt
        if let Ok(content) = helpers::read_file(project_path, "requirements.txt") {
            if content.contains("redis") {
                evidence.push(Evidence::Dependency {
                    name: "redis".into(),
                    version: None,
                    reason: "Redis client for Python".into(),
                });
                confidence += 0.4;
            }
        }

        // Check for redis-rs in Cargo.toml
        if let Ok(content) = helpers::read_file(project_path, "Cargo.toml") {
            if content.contains("redis") {
                evidence.push(Evidence::Dependency {
                    name: "redis".into(),
                    version: None,
                    reason: "Redis client for Rust".into(),
                });
                confidence += 0.4;
            }
        }

        // Search for connection strings
        if let Ok(pattern) = regex::Regex::new(r"redis://|localhost:6379") {
            if let Ok(results) =
                helpers::grep_in_project(project_path, &pattern, &["js", "ts", "py", "rs"])
            {
                if !results.is_empty() {
                    evidence.push(Evidence::FileContent {
                        path: results[0].path.clone(),
                        pattern: "redis://".into(),
                        line: results[0].line_num,
                        reason: "Redis connection string found".into(),
                    });
                    confidence += 0.3;
                }
            }
        }

        if confidence > 0.0 {
            let mut detection = Detection::new(Technology::Redis);
            for ev in evidence {
                detection = detection.add_evidence(ev);
            }
            detection.calculate_confidence();
            Ok(Some(detection))
        } else {
            Ok(None)
        }
    }

    fn priority(&self) -> u8 {
        50 // Medium priority
    }
}
