use anyhow::Result;

use super::Reporter;
use crate::types::ScanResults;

/// JSON reporter for machine-readable output
pub struct JsonReporter {
    pretty: bool,
}

impl JsonReporter {
    pub fn new() -> Self {
        Self { pretty: true }
    }

    pub fn compact() -> Self {
        Self { pretty: false }
    }
}

impl Reporter for JsonReporter {
    fn generate(&self, results: &ScanResults) -> Result<String> {
        let json = if self.pretty {
            serde_json::to_string_pretty(results)?
        } else {
            serde_json::to_string(results)?
        };
        Ok(json)
    }

    fn print(&self, results: &ScanResults) -> Result<()> {
        let output = self.generate(results)?;
        println!("{}", output);
        Ok(())
    }
}
