use std::path::Path;

/// Docker detector
/// Detects: Docker usage via Dockerfile, docker-compose.yml
use super::helpers;
use crate::error::DetectionResult;
use crate::models::{Detection, Evidence, Technology};

pub struct DockerDetector;

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

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

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

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

        // Check for Dockerfile
        if helpers::file_exists(project_path, "Dockerfile")? {
            evidence.push(Evidence::FileExists {
                path: project_path.join("Dockerfile"),
                reason: "Dockerfile is primary Docker indicator".into(),
            });
            confidence += 0.5;
        }

        // Check for .dockerignore
        if helpers::file_exists(project_path, ".dockerignore")? {
            evidence.push(Evidence::FileExists {
                path: project_path.join(".dockerignore"),
                reason: ".dockerignore suggests Docker setup".into(),
            });
            confidence += 0.3;
        }

        // Check for docker-compose.yml/yaml
        if helpers::file_exists(project_path, "docker-compose.yml")? {
            evidence.push(Evidence::FileExists {
                path: project_path.join("docker-compose.yml"),
                reason: "docker-compose.yml indicates Docker Compose".into(),
            });
            confidence += 0.4;
        }

        if helpers::file_exists(project_path, "docker-compose.yaml")? {
            evidence.push(Evidence::FileExists {
                path: project_path.join("docker-compose.yaml"),
                reason: "docker-compose.yaml indicates Docker Compose".into(),
            });
            confidence += 0.4;
        }

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

    fn priority(&self) -> u8 {
        60 // Higher priority - Dockerfile is very specific
    }
}
