use std::path::Path;
use super::models::{WizardFileChange, WizardFileChangeKind, WizardPlannedFile, WizardWriteResult};
pub fn collect_wizard_file_changes(
project_root: &Path,
files: &[WizardPlannedFile],
) -> Vec<WizardFileChange> {
files
.iter()
.map(|f| {
let path = project_root.join(&f.relative_path);
let kind = if !path.exists() {
WizardFileChangeKind::New
} else {
match std::fs::read_to_string(&path) {
Ok(existing) if existing == f.content => WizardFileChangeKind::Unchanged,
_ => WizardFileChangeKind::Update,
}
};
WizardFileChange {
relative_path: f.relative_path.clone(),
kind,
}
})
.collect()
}
pub fn write_wizard_files(
project_root: &Path,
files: &[WizardPlannedFile],
) -> Result<WizardWriteResult, std::io::Error> {
let mut written = Vec::new();
let mut unchanged = Vec::new();
for f in files {
let path = project_root.join(&f.relative_path);
if path.exists() {
if let Ok(existing) = std::fs::read_to_string(&path) {
if existing == f.content {
unchanged.push(f.relative_path.clone());
continue;
}
}
}
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&path, &f.content)?;
written.push(f.relative_path.clone());
}
Ok(WizardWriteResult { written, unchanged })
}