// DetectionRegistry implementation methods
// Included from mod.rs — shares parent module scope

impl DetectionRegistry {
    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new() -> Self {
        let mut registry = Self {
            detectors: std::collections::HashMap::new(),
        };

        // Register all available detection strategies
        registry.register_defaults();
        registry
    }

    fn register_defaults(&mut self) {
        // Register duplicate detector
        self.register("duplicates", Arc::new(duplicates::DuplicateDetector::new()));

        // Register SATD detector
        self.register("satd", Arc::new(satd::SATDDetector::new()));

        // Register polyglot analyzer
        self.register("polyglot", Arc::new(polyglot::PolyglotDetector::new()));
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Register a new item.
    pub fn register(
        &mut self,
        name: &str,
        detector: Arc<
            dyn Detector<
                Input = DetectionInput,
                Output = DetectionOutput,
                Config = DetectionConfig,
            >,
        >,
    ) {
        self.detectors.insert(name.to_string(), detector);
    }

    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Get detector.
    pub fn get_detector(
        &self,
        name: &str,
    ) -> Option<
        Arc<
            dyn Detector<
                Input = DetectionInput,
                Output = DetectionOutput,
                Config = DetectionConfig,
            >,
        >,
    > {
        self.detectors.get(name).cloned()
    }

    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// List detectors.
    pub fn list_detectors(&self) -> Vec<&str> {
        self.detectors
            .keys()
            .map(std::string::String::as_str)
            .collect()
    }

    /// Run detection using the specified detector
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub async fn detect(
        &self,
        detector_name: &str,
        input: DetectionInput,
        config: DetectionConfig,
    ) -> Result<DetectionOutput> {
        if let Some(detector) = self.get_detector(detector_name) {
            detector.detect(input, config).await
        } else {
            Err(anyhow::anyhow!("Unknown detector: {detector_name}"))
        }
    }
}

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

impl Default for DetectionConfig {
    fn default() -> Self {
        Self {
            max_files: None,
            parallel_processing: true,
            output_format: OutputFormat::Json,
            detector_specific: DetectorSpecificConfig::Duplicates(
                duplicates::DuplicateConfig::default(),
            ),
        }
    }
}
