use super::{Beacon, BeaconEmitter};
use std::path::PathBuf;

pub struct LocalBeacon {
    pub dir: PathBuf,
}

impl LocalBeacon {
    pub fn new(dir: PathBuf) -> Self {
        Self { dir }
    }
    pub fn default_path() -> PathBuf {
        let home = std::env::var("HOME").unwrap_or_else(|_| ".".into());
        PathBuf::from(home).join(".bctx").join("beacons")
    }
}

impl BeaconEmitter for LocalBeacon {
    fn emit(&self, beacon: &Beacon) {
        let _ = std::fs::create_dir_all(&self.dir);
        let name = format!("{}.json", beacon.timestamp.format("%Y%m%d_%H%M%S%f"));
        let _ = std::fs::write(
            self.dir.join(name),
            serde_json::to_string(beacon).unwrap_or_default(),
        );
    }
}
