use std::collections::{BTreeMap, BTreeSet};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::{fs, io};

use bone_kernel::{
    BrepSolid, DatumFeature, ExtrudeFeature, RevolveFeature, SweepFeature, SweepPath,
};
use bone_types::{
    AngleTolerance, BodyId, ChordHeightTolerance, DatumId, ExtrudeId, FeatureId, RevolveId,
    SchemaHeader, SchemaVersion, SketchId, SweepId,
};

use crate::document::{
    DatumFile, Document, DocumentHeader, ExtrudeFile, FeatureNode, FeatureTree, ImportedSolid,
    RevolveFile, SketchFile, SweepFile, body_brep_filename, body_labels_filename, datum_filename,
    extrude_filename, revolve_filename, sketch_filename, sweep_filename,
};
use crate::io::blob::{BlobHash, BlobKind};
use crate::io::labels::LabelSidecar;
use crate::io::ron_io::{RonError, from_str, to_string};
use crate::sketch3::Sketch3IntegrityError;

pub const DOCUMENT_FILE: &str = "document.ron";
pub const SKETCHES_DIR: &str = "sketches";
pub const EXTRUDES_DIR: &str = "extrudes";
pub const REVOLVES_DIR: &str = "revolves";
pub const SWEEPS_DIR: &str = "sweeps";
pub const DATUMS_DIR: &str = "datums";
pub const BODIES_DIR: &str = "bodies";
pub const BLOBS_DIR: &str = "blobs";
pub const CACHES_DIR: &str = "caches";
pub const TESSELLATIONS_DIR: &str = "tessellations";

const ROOT_GITIGNORE: &str = "caches/\n";
const ROOT_GITATTRIBUTES: &str = "* text=auto eol=lf\n*.brep text eol=lf\n*.labels text eol=lf\n";
const CACHES_GITIGNORE: &str = "*\n!.gitignore\n!CACHEDIR.TAG\n";
const CACHEDIR_TAG: &str = concat!(
    "Signature: 8a477f597d28d172789f06886806bc55\n",
    "# This file is a cache directory tag automatically created by bone.\n",
    "# For information about cache directory tags see https://bford.info/cachedir/\n",
);

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct FolderError(Box<FolderErrorKind>);

impl FolderError {
    #[must_use]
    pub fn kind(&self) -> &FolderErrorKind {
        &self.0
    }

    #[must_use]
    pub fn into_kind(self) -> FolderErrorKind {
        *self.0
    }
}

impl From<FolderErrorKind> for FolderError {
    fn from(kind: FolderErrorKind) -> Self {
        Self(Box::new(kind))
    }
}

impl FolderErrorKind {
    fn wrap(self) -> FolderError {
        FolderError(Box::new(self))
    }
}

#[derive(Debug, thiserror::Error)]
pub enum FolderErrorKind {
    #[error("io at {path}: {source}")]
    Io {
        path: PathBuf,
        #[source]
        source: io::Error,
    },
    #[error("ron at {path}: {source}")]
    Ron {
        path: PathBuf,
        #[source]
        source: RonError,
    },
    #[error("unknown schema {found} (expected name {expected_name})")]
    UnknownSchema {
        found: String,
        expected_name: &'static str,
    },
    #[error("schema {name} major v{found} is unsupported (this build supports v{supported})")]
    UnsupportedMajor {
        name: String,
        found: SchemaVersion,
        supported: SchemaVersion,
    },
    #[error("registry references sketch {id:?} with no file on disk")]
    MissingSketchFile { id: SketchId },
    #[error("integrity of {path}: {source}")]
    SketchIntegrity {
        path: PathBuf,
        #[source]
        source: Sketch3IntegrityError,
    },
    #[error("feature tree lists feature {id:?} more than once")]
    DuplicateFeatureId { id: FeatureId },
    #[error("feature tree has a dependency cycle through feature {id:?}")]
    FeatureCycle { id: FeatureId },
    #[error("feature {child:?} precedes its parent {parent:?} in feature order")]
    FeatureOrderViolation { parent: FeatureId, child: FeatureId },
    #[error("rollback marker references feature {id:?} absent from the feature tree")]
    DanglingRollback { id: FeatureId },
    #[error("rollback marker sits on datum feature {id:?}, which is never rollable")]
    RollbackOnDatum { id: FeatureId },
    #[error("suppressed set references feature {id:?} absent from the feature tree")]
    DanglingSuppressed { id: FeatureId },
    #[error("suppressed set lists datum feature {id:?}, which is never suppressible")]
    SuppressedDatum { id: FeatureId },
    #[error("feature tree references sketch {id:?} not in registry")]
    DanglingTreeSketch { id: SketchId },
    #[error("registry has sketch {id:?} absent from feature tree")]
    OrphanRegistered { id: SketchId },
    #[error("feature tree references extrude {id:?} with no file on disk")]
    MissingExtrudeFile { id: ExtrudeId },
    #[error("feature tree references revolve {id:?} with no file on disk")]
    MissingRevolveFile { id: RevolveId },
    #[error("feature tree references sweep {id:?} with no file on disk")]
    MissingSweepFile { id: SweepId },
    #[error("feature tree references datum {id:?} with no file on disk")]
    MissingDatumFile { id: DatumId },
    #[error("feature tree references imported body {id:?} with no blob on disk")]
    MissingBodyFile { id: BodyId },
    #[error("stored extrude {extrude:?} references sketch {sketch:?} absent from the document")]
    DanglingExtrudeSketch {
        extrude: ExtrudeId,
        sketch: SketchId,
    },
    #[error("stored revolve {revolve:?} references sketch {sketch:?} absent from the document")]
    DanglingRevolveSketch {
        revolve: RevolveId,
        sketch: SketchId,
    },
    #[error("stored sweep {sweep:?} references sketch {sketch:?} absent from the document")]
    DanglingSweepSketch { sweep: SweepId, sketch: SketchId },
    #[error("geometry blob at {path}: {source}")]
    Blob {
        path: PathBuf,
        #[source]
        source: bone_kernel::BrepError,
    },
    #[error("geometry blob at {path} hashes to {found}, expected {expected}")]
    BlobHashMismatch {
        path: PathBuf,
        found: String,
        expected: String,
    },
    #[error("label sidecar at {path} does not match its geometry blob")]
    SidecarMismatch { path: PathBuf },
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct DocumentFolder {
    path: PathBuf,
}

impl DocumentFolder {
    #[must_use]
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self { path: path.into() }
    }

    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    #[must_use]
    pub fn document_file(&self) -> PathBuf {
        self.path.join(DOCUMENT_FILE)
    }

    #[must_use]
    pub fn sketches_dir(&self) -> PathBuf {
        self.path.join(SKETCHES_DIR)
    }

    #[must_use]
    pub fn extrudes_dir(&self) -> PathBuf {
        self.path.join(EXTRUDES_DIR)
    }

    #[must_use]
    pub fn revolves_dir(&self) -> PathBuf {
        self.path.join(REVOLVES_DIR)
    }

    #[must_use]
    pub fn sweeps_dir(&self) -> PathBuf {
        self.path.join(SWEEPS_DIR)
    }

    #[must_use]
    pub fn datums_dir(&self) -> PathBuf {
        self.path.join(DATUMS_DIR)
    }

    #[must_use]
    pub fn bodies_dir(&self) -> PathBuf {
        self.path.join(BODIES_DIR)
    }

    #[must_use]
    pub fn blobs_dir(&self) -> PathBuf {
        self.path.join(BLOBS_DIR)
    }

    #[must_use]
    pub fn caches_dir(&self) -> PathBuf {
        self.path.join(CACHES_DIR)
    }

    #[must_use]
    pub fn sketch_path(&self, id: SketchId) -> PathBuf {
        self.sketches_dir().join(sketch_filename(id))
    }

    #[must_use]
    pub fn extrude_path(&self, id: ExtrudeId) -> PathBuf {
        self.extrudes_dir().join(extrude_filename(id))
    }

    #[must_use]
    pub fn revolve_path(&self, id: RevolveId) -> PathBuf {
        self.revolves_dir().join(revolve_filename(id))
    }

    #[must_use]
    pub fn sweep_path(&self, id: SweepId) -> PathBuf {
        self.sweeps_dir().join(sweep_filename(id))
    }

    #[must_use]
    pub fn datum_path(&self, id: DatumId) -> PathBuf {
        self.datums_dir().join(datum_filename(id))
    }

    #[must_use]
    pub fn body_brep_path(&self, id: BodyId) -> PathBuf {
        self.bodies_dir().join(body_brep_filename(id))
    }

    #[must_use]
    pub fn body_labels_path(&self, id: BodyId) -> PathBuf {
        self.bodies_dir().join(body_labels_filename(id))
    }

    #[must_use]
    pub fn blob_path(&self, hash: BlobHash, kind: BlobKind) -> PathBuf {
        self.blobs_dir().join(hash.relative_path(kind))
    }

    #[must_use]
    pub fn tessellation_path(
        &self,
        hash: BlobHash,
        chord: ChordHeightTolerance,
        angle: AngleTolerance,
    ) -> PathBuf {
        self.caches_dir().join(TESSELLATIONS_DIR).join(format!(
            "{}.{}.{}",
            hash.truncated_128_hex(),
            tessellation_tier_hex(chord, angle),
            BlobKind::TESS.as_str()
        ))
    }
}

fn tessellation_tier_hex(chord: ChordHeightTolerance, angle: AngleTolerance) -> String {
    format!(
        "{:016x}{:016x}",
        chord.millimeters().to_bits(),
        angle.radians().to_bits()
    )
}

pub fn save(document: &Document, folder: &DocumentFolder) -> Result<(), FolderError> {
    ensure_scaffold(folder)?;

    document
        .sketches()
        .try_for_each(|(id, sketch)| -> Result<(), FolderError> {
            let file = SketchFile::new(sketch.clone());
            let ron = to_ron(&folder.sketch_path(id), &file)?;
            write_if_different(&folder.sketch_path(id), &ron)
        })?;

    let tree_extrudes = tree_extrude_ids(document.header());
    document
        .header()
        .extrudes
        .iter()
        .filter(|(id, _)| tree_extrudes.contains(*id))
        .try_for_each(|(id, feature)| -> Result<(), FolderError> {
            let path = folder.extrude_path(*id);
            let label = document.extrude_label(*id).unwrap_or_default().to_owned();
            let ron = to_ron(&path, &ExtrudeFile::new(*feature, label))?;
            write_if_different(&path, &ron)
        })?;

    let tree_revolves = tree_revolve_ids(document.header());
    document
        .header()
        .revolves
        .iter()
        .filter(|(id, _)| tree_revolves.contains(*id))
        .try_for_each(|(id, feature)| -> Result<(), FolderError> {
            let path = folder.revolve_path(*id);
            let label = document.revolve_label(*id).unwrap_or_default().to_owned();
            let ron = to_ron(&path, &RevolveFile::new(*feature, label))?;
            write_if_different(&path, &ron)
        })?;

    let tree_sweeps = tree_sweep_ids(document.header());
    document
        .header()
        .sweeps
        .iter()
        .filter(|(id, _)| tree_sweeps.contains(*id))
        .try_for_each(|(id, feature)| -> Result<(), FolderError> {
            let path = folder.sweep_path(*id);
            let label = document.sweep_label(*id).unwrap_or_default().to_owned();
            let ron = to_ron(&path, &SweepFile::new(feature.clone(), label))?;
            write_if_different(&path, &ron)
        })?;

    let tree_datums = tree_datum_ids(document.header());
    document
        .header()
        .datums
        .iter()
        .filter(|(id, _)| tree_datums.contains(*id))
        .try_for_each(|(id, feature)| -> Result<(), FolderError> {
            let path = folder.datum_path(*id);
            let label = document.datum_label(*id).unwrap_or_default().to_owned();
            let ron = to_ron(&path, &DatumFile::new(feature.clone(), label))?;
            write_if_different(&path, &ron)
        })?;

    document
        .imported_bodies()
        .try_for_each(|(id, solid)| write_body(folder, id, solid))?;

    let document_ron = to_ron(&folder.document_file(), document.header())?;
    write_if_different(&folder.document_file(), &document_ron)?;

    let live_sketches = document
        .registry()
        .order()
        .iter()
        .copied()
        .map(sketch_filename)
        .collect();
    remove_stale_files(&folder.sketches_dir(), &live_sketches, is_ron)?;
    let live_extrudes = tree_extrudes
        .iter()
        .copied()
        .map(extrude_filename)
        .collect();
    remove_stale_files(&folder.extrudes_dir(), &live_extrudes, is_ron)?;
    let live_revolves = tree_revolves
        .iter()
        .copied()
        .map(revolve_filename)
        .collect();
    remove_stale_files(&folder.revolves_dir(), &live_revolves, is_ron)?;
    let live_sweeps = tree_sweeps.iter().copied().map(sweep_filename).collect();
    remove_stale_files(&folder.sweeps_dir(), &live_sweeps, is_ron)?;
    let live_datums = tree_datums.iter().copied().map(datum_filename).collect();
    remove_stale_files(&folder.datums_dir(), &live_datums, is_ron)?;
    let live_bodies = tree_body_ids(document.header())
        .iter()
        .flat_map(|id| [body_brep_filename(*id), body_labels_filename(*id)])
        .collect();
    remove_stale_files(&folder.bodies_dir(), &live_bodies, is_body_blob)?;

    Ok(())
}

fn write_body(folder: &DocumentFolder, id: BodyId, solid: &BrepSolid) -> Result<(), FolderError> {
    let brep_path = folder.body_brep_path(id);
    let labels_path = folder.body_labels_path(id);
    let blob = solid.to_blob().map_err(|source| {
        FolderErrorKind::Blob {
            path: brep_path.clone(),
            source,
        }
        .wrap()
    })?;
    let sidecar = LabelSidecar::capture(solid).to_ron().map_err(|source| {
        FolderErrorKind::Ron {
            path: labels_path.clone(),
            source,
        }
        .wrap()
    })?;
    write_bytes_if_different(&brep_path, &blob)?;
    write_bytes_if_different(&labels_path, sidecar.as_bytes())
}

fn is_ron(ext: &str) -> bool {
    ext.eq_ignore_ascii_case("ron")
}

fn is_body_blob(ext: &str) -> bool {
    ext.eq_ignore_ascii_case("brep") || ext.eq_ignore_ascii_case("labels")
}

fn tree_extrude_ids(header: &DocumentHeader) -> BTreeSet<ExtrudeId> {
    header
        .feature_tree
        .iter()
        .filter_map(|(_, node)| match node {
            FeatureNode::Extrude(id) => Some(id),
            FeatureNode::Origin
            | FeatureNode::PrincipalPlane(_)
            | FeatureNode::Sketch(_)
            | FeatureNode::Revolve(_)
            | FeatureNode::Sweep(_)
            | FeatureNode::Datum(_)
            | FeatureNode::ImportedBody(_) => None,
        })
        .collect()
}

fn tree_revolve_ids(header: &DocumentHeader) -> BTreeSet<RevolveId> {
    header
        .feature_tree
        .iter()
        .filter_map(|(_, node)| match node {
            FeatureNode::Revolve(id) => Some(id),
            FeatureNode::Origin
            | FeatureNode::PrincipalPlane(_)
            | FeatureNode::Sketch(_)
            | FeatureNode::Extrude(_)
            | FeatureNode::Sweep(_)
            | FeatureNode::Datum(_)
            | FeatureNode::ImportedBody(_) => None,
        })
        .collect()
}

fn tree_sweep_ids(header: &DocumentHeader) -> BTreeSet<SweepId> {
    header
        .feature_tree
        .iter()
        .filter_map(|(_, node)| match node {
            FeatureNode::Sweep(id) => Some(id),
            FeatureNode::Origin
            | FeatureNode::PrincipalPlane(_)
            | FeatureNode::Sketch(_)
            | FeatureNode::Extrude(_)
            | FeatureNode::Revolve(_)
            | FeatureNode::Datum(_)
            | FeatureNode::ImportedBody(_) => None,
        })
        .collect()
}

fn tree_datum_ids(header: &DocumentHeader) -> BTreeSet<DatumId> {
    header
        .feature_tree
        .iter()
        .filter_map(|(_, node)| match node {
            FeatureNode::Datum(id) => Some(id),
            FeatureNode::Origin
            | FeatureNode::PrincipalPlane(_)
            | FeatureNode::Sketch(_)
            | FeatureNode::Extrude(_)
            | FeatureNode::Revolve(_)
            | FeatureNode::Sweep(_)
            | FeatureNode::ImportedBody(_) => None,
        })
        .collect()
}

fn tree_body_ids(header: &DocumentHeader) -> BTreeSet<BodyId> {
    header
        .feature_tree
        .iter()
        .filter_map(|(_, node)| match node {
            FeatureNode::ImportedBody(id) => Some(id),
            FeatureNode::Origin
            | FeatureNode::PrincipalPlane(_)
            | FeatureNode::Sketch(_)
            | FeatureNode::Extrude(_)
            | FeatureNode::Revolve(_)
            | FeatureNode::Sweep(_)
            | FeatureNode::Datum(_) => None,
        })
        .collect()
}

pub fn load(folder: &DocumentFolder) -> Result<Document, FolderError> {
    let header_path = folder.document_file();
    let header_text = read_to_string(&header_path)?;
    check_schema(&peek_schema(&header_path, &header_text)?)?;
    let mut header: DocumentHeader = from_ron(&header_path, &header_text)?;
    let (extrudes, extrude_labels) = read_extrudes(folder, &header)?;
    header.extrudes = extrudes;
    header.extrude_labels = extrude_labels;
    let (revolves, revolve_labels) = read_revolves(folder, &header)?;
    header.revolves = revolves;
    header.revolve_labels = revolve_labels;
    let (sweeps, sweep_labels) = read_sweeps(folder, &header)?;
    header.sweeps = sweeps;
    header.sweep_labels = sweep_labels;
    let (datums, datum_labels) = read_datums(folder, &header)?;
    header.datums = datums;
    header.datum_labels = datum_labels;
    validate_header(&header)?;

    let sketches =
        header
            .sketches
            .order()
            .iter()
            .copied()
            .try_fold(BTreeMap::new(), |mut acc, id| {
                let path = folder.sketch_path(id);
                let text = read_to_string(&path).map_err(|e| match e.into_kind() {
                    FolderErrorKind::Io { source, .. }
                        if source.kind() == io::ErrorKind::NotFound =>
                    {
                        FolderErrorKind::MissingSketchFile { id }.wrap()
                    }
                    other => other.wrap(),
                })?;
                let file: SketchFile = from_ron(&path, &text)?;
                check_schema(&file.schema)?;
                file.sketch.validate().map_err(|source| {
                    FolderErrorKind::SketchIntegrity {
                        path: path.clone(),
                        source,
                    }
                    .wrap()
                })?;
                acc.insert(id, file.sketch);
                Ok::<_, FolderError>(acc)
            })?;

    let bodies = read_bodies(folder, &header)?;

    let document = Document::from_parts(header, sketches, bodies);
    ensure_acyclic(document.feature_tree())?;
    ensure_ordered(document.feature_tree())?;
    Ok(document)
}

fn ensure_acyclic(tree: &FeatureTree) -> Result<(), FolderError> {
    match tree.find_cycle() {
        Some(id) => Err(FolderErrorKind::FeatureCycle { id }.wrap()),
        None => Ok(()),
    }
}

fn ensure_ordered(tree: &FeatureTree) -> Result<(), FolderError> {
    match tree.order_violation() {
        Some(edge) => {
            let (parent, child) = edge.endpoints();
            Err(FolderErrorKind::FeatureOrderViolation { parent, child }.wrap())
        }
        None => Ok(()),
    }
}

fn read_bodies(
    folder: &DocumentFolder,
    header: &DocumentHeader,
) -> Result<BTreeMap<BodyId, ImportedSolid>, FolderError> {
    tree_body_ids(header)
        .into_iter()
        .try_fold(BTreeMap::new(), |mut acc, id| {
            acc.insert(id, ImportedSolid::new(read_body(folder, id)?));
            Ok::<_, FolderError>(acc)
        })
}

fn read_body(folder: &DocumentFolder, id: BodyId) -> Result<BrepSolid, FolderError> {
    let brep_path = folder.body_brep_path(id);
    let labels_path = folder.body_labels_path(id);
    let blob = read_bytes(&brep_path).map_err(|e| missing_body(e, id))?;
    let sidecar_text = read_to_string(&labels_path).map_err(|e| missing_body(e, id))?;
    let sidecar = LabelSidecar::from_ron(&sidecar_text).map_err(|source| {
        FolderErrorKind::Ron {
            path: labels_path.clone(),
            source,
        }
        .wrap()
    })?;
    let solid = BrepSolid::from_blob(&blob, sidecar.reattach()).map_err(|source| {
        FolderErrorKind::Blob {
            path: brep_path,
            source,
        }
        .wrap()
    })?;
    if sidecar.matches(solid.content_key()) {
        Ok(solid)
    } else {
        Err(FolderErrorKind::SidecarMismatch { path: labels_path }.wrap())
    }
}

fn missing_body(error: FolderError, id: BodyId) -> FolderError {
    match error.into_kind() {
        FolderErrorKind::Io { source, .. } if source.kind() == io::ErrorKind::NotFound => {
            FolderErrorKind::MissingBodyFile { id }.wrap()
        }
        other => other.wrap(),
    }
}

type ExtrudeData = (
    BTreeMap<ExtrudeId, ExtrudeFeature>,
    BTreeMap<ExtrudeId, String>,
);

fn read_extrudes(
    folder: &DocumentFolder,
    header: &DocumentHeader,
) -> Result<ExtrudeData, FolderError> {
    tree_extrude_ids(header).into_iter().try_fold(
        (BTreeMap::new(), BTreeMap::new()),
        |(mut features, mut labels), id| {
            let path = folder.extrude_path(id);
            let text = read_to_string(&path).map_err(|e| match e.into_kind() {
                FolderErrorKind::Io { source, .. } if source.kind() == io::ErrorKind::NotFound => {
                    FolderErrorKind::MissingExtrudeFile { id }.wrap()
                }
                other => other.wrap(),
            })?;
            let file: ExtrudeFile = from_ron(&path, &text)?;
            check_schema(&file.schema)?;
            features.insert(id, file.feature);
            labels.insert(id, file.label);
            Ok::<_, FolderError>((features, labels))
        },
    )
}

type RevolveData = (
    BTreeMap<RevolveId, RevolveFeature>,
    BTreeMap<RevolveId, String>,
);

fn read_revolves(
    folder: &DocumentFolder,
    header: &DocumentHeader,
) -> Result<RevolveData, FolderError> {
    tree_revolve_ids(header).into_iter().try_fold(
        (BTreeMap::new(), BTreeMap::new()),
        |(mut features, mut labels), id| {
            let path = folder.revolve_path(id);
            let text = read_to_string(&path).map_err(|e| match e.into_kind() {
                FolderErrorKind::Io { source, .. } if source.kind() == io::ErrorKind::NotFound => {
                    FolderErrorKind::MissingRevolveFile { id }.wrap()
                }
                other => other.wrap(),
            })?;
            let file: RevolveFile = from_ron(&path, &text)?;
            check_schema(&file.schema)?;
            features.insert(id, file.feature);
            labels.insert(id, file.label);
            Ok::<_, FolderError>((features, labels))
        },
    )
}

type SweepData = (BTreeMap<SweepId, SweepFeature>, BTreeMap<SweepId, String>);

fn read_sweeps(folder: &DocumentFolder, header: &DocumentHeader) -> Result<SweepData, FolderError> {
    tree_sweep_ids(header).into_iter().try_fold(
        (BTreeMap::new(), BTreeMap::new()),
        |(mut features, mut labels), id| {
            let path = folder.sweep_path(id);
            let text = read_to_string(&path).map_err(|e| match e.into_kind() {
                FolderErrorKind::Io { source, .. } if source.kind() == io::ErrorKind::NotFound => {
                    FolderErrorKind::MissingSweepFile { id }.wrap()
                }
                other => other.wrap(),
            })?;
            let file: SweepFile = from_ron(&path, &text)?;
            check_schema(&file.schema)?;
            features.insert(id, file.feature);
            labels.insert(id, file.label);
            Ok::<_, FolderError>((features, labels))
        },
    )
}

type DatumData = (BTreeMap<DatumId, DatumFeature>, BTreeMap<DatumId, String>);

fn read_datums(folder: &DocumentFolder, header: &DocumentHeader) -> Result<DatumData, FolderError> {
    tree_datum_ids(header).into_iter().try_fold(
        (BTreeMap::new(), BTreeMap::new()),
        |(mut features, mut labels), id| {
            let path = folder.datum_path(id);
            let text = read_to_string(&path).map_err(|e| match e.into_kind() {
                FolderErrorKind::Io { source, .. } if source.kind() == io::ErrorKind::NotFound => {
                    FolderErrorKind::MissingDatumFile { id }.wrap()
                }
                other => other.wrap(),
            })?;
            let file: DatumFile = from_ron(&path, &text)?;
            check_schema(&file.schema)?;
            features.insert(id, file.feature);
            labels.insert(id, file.label);
            Ok::<_, FolderError>((features, labels))
        },
    )
}

fn validate_header(header: &DocumentHeader) -> Result<(), FolderError> {
    let tree = &header.feature_tree;

    let duplicate = tree
        .iter()
        .map(|(id, _)| id)
        .scan(BTreeSet::new(), |seen, id| Some((id, !seen.insert(id))))
        .find_map(|(id, repeated)| repeated.then_some(id));
    if let Some(id) = duplicate {
        return Err(FolderErrorKind::DuplicateFeatureId { id }.wrap());
    }

    let registered: BTreeSet<SketchId> = header.sketches.order().iter().copied().collect();
    let tree_sketches: BTreeSet<SketchId> = tree
        .iter()
        .filter_map(|(_, node)| match node {
            FeatureNode::Sketch(id) => Some(id),
            FeatureNode::Origin
            | FeatureNode::PrincipalPlane(_)
            | FeatureNode::Extrude(_)
            | FeatureNode::Revolve(_)
            | FeatureNode::Sweep(_)
            | FeatureNode::Datum(_)
            | FeatureNode::ImportedBody(_) => None,
        })
        .collect();
    if let Some(&id) = tree_sketches.difference(&registered).next() {
        return Err(FolderErrorKind::DanglingTreeSketch { id }.wrap());
    }
    if let Some(&id) = registered.difference(&tree_sketches).next() {
        return Err(FolderErrorKind::OrphanRegistered { id }.wrap());
    }

    if let Some((&extrude, feature)) = header
        .extrudes
        .iter()
        .find(|(_, feature)| !registered.contains(&feature.sketch))
    {
        return Err(FolderErrorKind::DanglingExtrudeSketch {
            extrude,
            sketch: feature.sketch,
        }
        .wrap());
    }

    if let Some((&revolve, feature)) = header
        .revolves
        .iter()
        .find(|(_, feature)| !registered.contains(&feature.sketch))
    {
        return Err(FolderErrorKind::DanglingRevolveSketch {
            revolve,
            sketch: feature.sketch,
        }
        .wrap());
    }

    let unregistered_rail = |rail: &SweepPath| match rail {
        SweepPath::Sketch(path) if !registered.contains(path) => Some(*path),
        _ => None,
    };
    if let Some((&sweep, feature)) = header.sweeps.iter().find(|(_, feature)| {
        !registered.contains(&feature.profile)
            || unregistered_rail(&feature.path).is_some()
            || feature
                .guides
                .iter()
                .any(|guide| unregistered_rail(guide).is_some())
    }) {
        let sketch = unregistered_rail(&feature.path)
            .or_else(|| feature.guides.iter().find_map(unregistered_rail))
            .unwrap_or(feature.profile);
        return Err(FolderErrorKind::DanglingSweepSketch { sweep, sketch }.wrap());
    }

    let live: BTreeSet<FeatureId> = tree.iter().map(|(id, _)| id).collect();
    if let Some(id) = header.rollback.feature() {
        if !live.contains(&id) {
            return Err(FolderErrorKind::DanglingRollback { id }.wrap());
        }
        if tree.is_seeded_reference(id) {
            return Err(FolderErrorKind::RollbackOnDatum { id }.wrap());
        }
    }
    if let Some(&id) = header.suppressed.difference(&live).next() {
        return Err(FolderErrorKind::DanglingSuppressed { id }.wrap());
    }
    if let Some(&id) = header
        .suppressed
        .iter()
        .find(|&&id| tree.is_seeded_reference(id))
    {
        return Err(FolderErrorKind::SuppressedDatum { id }.wrap());
    }

    Ok(())
}

#[derive(serde::Deserialize)]
#[serde(rename = "DocumentHeader")]
struct SchemaProbe {
    schema: SchemaHeader,
}

fn peek_schema(path: &Path, text: &str) -> Result<SchemaHeader, FolderError> {
    from_ron::<SchemaProbe>(path, text).map(|probe| probe.schema)
}

fn check_schema(schema: &SchemaHeader) -> Result<(), FolderError> {
    if !schema.is_bone_document() {
        return Err(FolderErrorKind::UnknownSchema {
            found: schema.name.clone(),
            expected_name: SchemaHeader::BONE_DOCUMENT_NAME,
        }
        .wrap());
    }
    let supported = SchemaVersion::new(
        SchemaHeader::BONE_DOCUMENT_MAJOR,
        SchemaHeader::BONE_DOCUMENT_MINOR,
    );
    if schema.version.major != SchemaHeader::BONE_DOCUMENT_MAJOR {
        return Err(FolderErrorKind::UnsupportedMajor {
            name: schema.name.clone(),
            found: schema.version,
            supported,
        }
        .wrap());
    }
    if schema.version.minor > SchemaHeader::BONE_DOCUMENT_MINOR {
        tracing::info!(
            name = %schema.name,
            found = %schema.version,
            supported = %supported,
            "accepting a newer minor schema version than this build writes"
        );
    }
    Ok(())
}

pub(crate) fn ensure_scaffold(folder: &DocumentFolder) -> Result<(), FolderError> {
    write_if_different(&folder.path().join(".gitignore"), ROOT_GITIGNORE)?;
    write_if_different(&folder.path().join(".gitattributes"), ROOT_GITATTRIBUTES)?;
    write_if_different(&folder.caches_dir().join("CACHEDIR.TAG"), CACHEDIR_TAG)?;
    write_if_different(&folder.caches_dir().join(".gitignore"), CACHES_GITIGNORE)
}

pub(crate) fn ensure_dir(path: &Path) -> Result<(), FolderError> {
    fs::create_dir_all(path).map_err(|source| {
        FolderErrorKind::Io {
            path: path.to_path_buf(),
            source,
        }
        .wrap()
    })
}

fn write_if_different(path: &Path, contents: &str) -> Result<(), FolderError> {
    if let Ok(existing) = fs::read_to_string(path)
        && existing == contents
    {
        return Ok(());
    }
    atomic_write(path, contents)
}

fn write_bytes_if_different(path: &Path, contents: &[u8]) -> Result<(), FolderError> {
    if let Ok(existing) = fs::read(path)
        && existing == contents
    {
        return Ok(());
    }
    atomic_write_bytes(path, contents)
}

pub(crate) fn write_if_absent(path: &Path, contents: &[u8]) -> Result<(), FolderError> {
    match path.try_exists() {
        Ok(true) => Ok(()),
        Ok(false) => atomic_write_bytes(path, contents),
        Err(source) => Err(FolderErrorKind::Io {
            path: path.to_path_buf(),
            source,
        }
        .wrap()),
    }
}

fn atomic_write(path: &Path, contents: &str) -> Result<(), FolderError> {
    atomic_write_bytes(path, contents.as_bytes())
}

#[derive(Copy, Clone)]
enum Durability {
    Fsync,
    Fast,
}

pub(crate) fn atomic_write_bytes(path: &Path, contents: &[u8]) -> Result<(), FolderError> {
    atomic_write_with(path, contents, Durability::Fsync)
}

pub(crate) fn atomic_write_cache(path: &Path, contents: &[u8]) -> Result<(), FolderError> {
    atomic_write_with(path, contents, Durability::Fast)
}

fn atomic_write_with(
    path: &Path,
    contents: &[u8],
    durability: Durability,
) -> Result<(), FolderError> {
    let parent = path.parent().unwrap_or_else(|| Path::new("."));
    ensure_dir(parent)?;
    let tmp = tmp_sibling(path);
    write_tmp(&tmp, contents, durability)?;
    fs::rename(&tmp, path).map_err(|source| {
        let _ = fs::remove_file(&tmp);
        FolderErrorKind::Io {
            path: path.to_path_buf(),
            source,
        }
        .wrap()
    })?;
    match durability {
        Durability::Fsync => sync_dir(parent),
        Durability::Fast => Ok(()),
    }
}

fn write_tmp(path: &Path, contents: &[u8], durability: Durability) -> Result<(), FolderError> {
    let mut file = fs::File::create(path).map_err(|source| {
        FolderErrorKind::Io {
            path: path.to_path_buf(),
            source,
        }
        .wrap()
    })?;
    file.write_all(contents).map_err(|source| {
        FolderErrorKind::Io {
            path: path.to_path_buf(),
            source,
        }
        .wrap()
    })?;
    match durability {
        Durability::Fsync => file.sync_all().map_err(|source| {
            FolderErrorKind::Io {
                path: path.to_path_buf(),
                source,
            }
            .wrap()
        }),
        Durability::Fast => Ok(()),
    }
}

pub(crate) fn read_bytes(path: &Path) -> Result<Vec<u8>, FolderError> {
    fs::read(path).map_err(|source| {
        FolderErrorKind::Io {
            path: path.to_path_buf(),
            source,
        }
        .wrap()
    })
}

#[cfg(unix)]
fn sync_dir(path: &Path) -> Result<(), FolderError> {
    fs::File::open(path)
        .and_then(|f| f.sync_all())
        .map_err(|source| {
            FolderErrorKind::Io {
                path: path.to_path_buf(),
                source,
            }
            .wrap()
        })
}

#[cfg(not(unix))]
fn sync_dir(_: &Path) -> Result<(), FolderError> {
    Ok(())
}

static TMP_SEQUENCE: AtomicU64 = AtomicU64::new(0);

fn tmp_sibling(path: &Path) -> PathBuf {
    let file_name = path
        .file_name()
        .map(std::ffi::OsStr::to_os_string)
        .unwrap_or_default();
    let sequence = TMP_SEQUENCE.fetch_add(1, Ordering::Relaxed);
    let mut tmp_name = file_name;
    tmp_name.push(format!(".{}.{sequence}.tmp", std::process::id()));
    path.with_file_name(tmp_name)
}

pub(crate) fn read_to_string(path: &Path) -> Result<String, FolderError> {
    fs::read_to_string(path).map_err(|source| {
        FolderErrorKind::Io {
            path: path.to_path_buf(),
            source,
        }
        .wrap()
    })
}

fn to_ron<T: serde::Serialize>(path: &Path, value: &T) -> Result<String, FolderError> {
    to_string(value).map_err(|source| {
        FolderErrorKind::Ron {
            path: path.to_path_buf(),
            source,
        }
        .wrap()
    })
}

fn from_ron<T: serde::de::DeserializeOwned>(path: &Path, text: &str) -> Result<T, FolderError> {
    from_str(text).map_err(|source| {
        FolderErrorKind::Ron {
            path: path.to_path_buf(),
            source,
        }
        .wrap()
    })
}

fn remove_stale_files(
    dir: &Path,
    live_names: &BTreeSet<String>,
    is_managed_ext: impl Fn(&str) -> bool,
) -> Result<(), FolderError> {
    let entries = match fs::read_dir(dir) {
        Ok(iter) => iter,
        Err(ref source) if source.kind() == io::ErrorKind::NotFound => return Ok(()),
        Err(source) => {
            return Err(FolderErrorKind::Io {
                path: dir.to_path_buf(),
                source,
            }
            .into());
        }
    };
    let modified = entries.into_iter().try_fold(false, |modified, entry| {
        let entry = entry.map_err(|source| {
            FolderErrorKind::Io {
                path: dir.to_path_buf(),
                source,
            }
            .wrap()
        })?;
        let name = entry.file_name().to_string_lossy().into_owned();
        let stale = Path::new(&name)
            .extension()
            .and_then(|ext| ext.to_str())
            .is_some_and(&is_managed_ext)
            && !live_names.contains(&name);
        if stale {
            let path = entry.path();
            fs::remove_file(&path).map_err(|source| FolderErrorKind::Io { path, source }.wrap())?;
            Ok::<_, FolderError>(true)
        } else {
            Ok(modified)
        }
    })?;
    if modified {
        sync_dir(dir)?;
    }
    Ok(())
}

#[cfg(test)]
mod tree_sourced_save {
    use super::{DocumentFolder, ensure_dir, load, save};
    use crate::document::{Document, DocumentHeader};
    use bone_kernel::ExtrudeFeature;
    use bone_types::{DocumentId, ExtrudeId, Length, PositiveLength, SketchId, millimeter};
    use slotmap::{Key, KeyData};
    use std::collections::BTreeMap;

    fn extrude_id(idx: u32) -> ExtrudeId {
        ExtrudeId::from(KeyData::from_ffi((1u64 << 32) | u64::from(idx)))
    }

    fn document_id(idx: u32) -> DocumentId {
        DocumentId::from(KeyData::from_ffi((1u64 << 32) | u64::from(idx)))
    }

    fn blind(sketch: SketchId) -> ExtrudeFeature {
        let Ok(depth) = PositiveLength::new(Length::new::<millimeter>(10.0)) else {
            panic!("positive depth");
        };
        ExtrudeFeature::blind(sketch, depth)
    }

    #[test]
    fn extrude_in_map_without_tree_node_is_not_persisted_and_stale_file_is_reaped() {
        let Ok(dir) = tempfile::tempdir() else {
            panic!("tempdir");
        };
        let folder = DocumentFolder::new(dir.path().join("orphan.bone"));

        let mut header = DocumentHeader::new(document_id(1), "orphan".to_owned());
        let orphan = extrude_id(1);
        header.extrudes.insert(orphan, blind(SketchId::null()));
        assert!(
            header.feature_tree.feature_of_extrude(orphan).is_none(),
            "the orphan starts with no feature-tree node"
        );
        let doc = Document::from_parts(header, BTreeMap::new(), BTreeMap::new());

        let Ok(()) = save(&doc, &folder) else {
            panic!("save");
        };
        assert!(
            !folder.extrude_path(orphan).exists(),
            "an extrude absent from the feature tree is not part of the document and is not written"
        );

        let Ok(()) = ensure_dir(&folder.extrudes_dir()) else {
            panic!("extrudes dir");
        };
        let Ok(()) = std::fs::write(folder.extrude_path(orphan), "ExtrudeFile()") else {
            panic!("plant stale file");
        };
        let Ok(()) = save(&doc, &folder) else {
            panic!("resave");
        };
        assert!(
            !folder.extrude_path(orphan).exists(),
            "a stale extrude file with no tree node is reaped even while the map still lists it"
        );

        let Ok(loaded) = load(&folder) else {
            panic!("load");
        };
        assert!(!loaded.header().extrudes.contains_key(&orphan));
    }

    #[test]
    fn validate_header_rejects_duplicate_feature_id() {
        use super::{FolderErrorKind, validate_header};
        use bone_types::BodyId;

        let mut header = DocumentHeader::new(document_id(1), "dup".to_owned());
        let Some((existing, _)) = header.feature_tree.iter().next() else {
            panic!("the seeded tree has feature nodes");
        };
        let body = BodyId::from(KeyData::from_ffi((1u64 << 32) | 1));
        header.feature_tree.push_imported_body(existing, body);

        let Err(err) = validate_header(&header) else {
            panic!("a feature tree with a repeated id must be rejected");
        };
        assert!(matches!(
            err.into_kind(),
            FolderErrorKind::DuplicateFeatureId { id } if id == existing
        ));
    }

    #[test]
    fn validate_header_rejects_rollback_on_a_datum() {
        use super::{FolderErrorKind, validate_header};
        use bone_types::RollbackMarker;

        let mut header = DocumentHeader::new(document_id(1), "datum".to_owned());
        let Some((datum, _)) = header.feature_tree.iter().next() else {
            panic!("the seeded tree opens with a datum");
        };
        header.rollback = RollbackMarker::Above(datum);

        let Err(err) = validate_header(&header) else {
            panic!("a rollback marker sitting on a datum must be rejected");
        };
        assert!(matches!(
            err.into_kind(),
            FolderErrorKind::RollbackOnDatum { id } if id == datum
        ));
    }

    #[test]
    fn a_three_d_sketch_and_its_revolve_axis_edge_round_trip() {
        use crate::sketch3::{Edit3Outcome, Sketch3, Sketch3Edit, Sketch3Entity, Sketch3Relation};
        use bone_kernel::{RevolveAngle, RevolveAxis, RevolveFeature};
        use bone_types::{Point3, SketchPlaneBasis, Tolerance, UnitVec3};

        let Ok(dir) = tempfile::tempdir() else {
            panic!("tempdir");
        };
        let folder = DocumentFolder::new(dir.path().join("sketch3.bone"));

        let entity = |outcome| match outcome {
            Edit3Outcome::Entity(id) => id,
            Edit3Outcome::Relation(_) | Edit3Outcome::None => {
                panic!("expected an entity outcome")
            }
        };
        let (sketch, start) = Sketch3::new().apply(Sketch3Edit::AddEntity(Sketch3Entity::point(
            Point3::origin(),
        )));
        let start = entity(start);
        let (sketch, end) = sketch.apply(Sketch3Edit::AddEntity(Sketch3Entity::point(
            Point3::from_mm(0.0, 7.0, 0.0),
        )));
        let end = entity(end);
        let (sketch, line) = sketch.apply(Sketch3Edit::AddEntity(Sketch3Entity::line(
            start, end, false,
        )));
        let line = entity(line);
        let (sketch, _) = sketch.apply(Sketch3Edit::AddRelation(Sketch3Relation::Fix(start)));
        let (sketch, _) = sketch.apply(Sketch3Edit::AddRelation(Sketch3Relation::AlongAxis {
            line,
            axis: UnitVec3::y_axis(),
        }));
        let (sketch3, _) = sketch.apply(Sketch3Edit::AddRelation(Sketch3Relation::Distance {
            a: start,
            b: end,
            value: Length::new::<millimeter>(7.0),
            reference: false,
        }));

        let mut document = Document::new(document_id(1), "sketch3".to_owned());
        let sketch3_id = document.allocate_sketch();
        document.insert_sketch(sketch3_id, "Sketch3D".to_owned(), sketch3.clone());
        let Some(sketch3_feature) = document.feature_tree().feature_of_sketch(sketch3_id) else {
            panic!("the sketch3 has a feature node");
        };

        let Ok(basis) = SketchPlaneBasis::new(
            Point3::origin(),
            UnitVec3::x_axis(),
            UnitVec3::y_axis(),
            Tolerance::new(1.0e-9),
        ) else {
            panic!("the xy basis is orthonormal");
        };
        let profile_id = document.allocate_sketch();
        document.insert_sketch(profile_id, "Profile".to_owned(), Sketch3::on_plane(basis));

        let revolve_id = document.commit_revolve(RevolveFeature::blind(
            profile_id,
            RevolveAxis::Sketch3Line {
                sketch: sketch3_feature,
                entity: line,
            },
            RevolveAngle::full(),
        ));

        let Ok(()) = save(&document, &folder) else {
            panic!("save");
        };
        assert!(
            folder.sketch_path(sketch3_id).exists(),
            "the 3D sketch is written to disk"
        );

        let Ok(loaded) = load(&folder) else {
            panic!("load");
        };
        let Some(restored) = loaded.sketch3(sketch3_id) else {
            panic!("the 3D sketch survives the round trip");
        };
        assert_eq!(
            restored, &sketch3,
            "the restored 3D sketch matches what was saved"
        );

        let Some(revolve_feature) = loaded.feature_tree().feature_of_revolve(revolve_id) else {
            panic!("the revolve survives the round trip");
        };
        assert!(
            loaded
                .feature_tree()
                .parents(revolve_feature)
                .contains(&sketch3_feature),
            "the 3D-sketch axis dependency edge is reconstructed on load",
        );
    }

    #[test]
    fn a_sweep_and_its_path_edge_round_trip() {
        use crate::sketch3::{Edit3Outcome, Sketch3, Sketch3Edit, Sketch3Entity};
        use bone_kernel::{SweepFeature, SweepPath};
        use bone_types::{Point3, SketchPlaneBasis, Tolerance, UnitVec3};

        let Ok(dir) = tempfile::tempdir() else {
            panic!("tempdir");
        };
        let folder = DocumentFolder::new(dir.path().join("sweep.bone"));

        let entity = |outcome| match outcome {
            Edit3Outcome::Entity(id) => id,
            Edit3Outcome::Relation(_) | Edit3Outcome::None => panic!("expected an entity outcome"),
        };
        let (path_sketch, center) = Sketch3::new().apply(Sketch3Edit::AddEntity(
            Sketch3Entity::point(Point3::origin()),
        ));
        let center = entity(center);
        let (path_sketch, _) = path_sketch.apply(Sketch3Edit::AddEntity(Sketch3Entity::circle(
            center,
            Length::new::<millimeter>(5.0),
            UnitVec3::y_axis(),
            false,
        )));

        let mut document = Document::new(document_id(1), "sweep".to_owned());
        let path_id = document.allocate_sketch();
        document.insert_sketch(path_id, "Path".to_owned(), path_sketch.clone());
        let Some(path_feature) = document.feature_tree().feature_of_sketch(path_id) else {
            panic!("the path sketch has a feature node");
        };

        let Ok(basis) = SketchPlaneBasis::new(
            Point3::origin(),
            UnitVec3::x_axis(),
            UnitVec3::y_axis(),
            Tolerance::new(1.0e-9),
        ) else {
            panic!("the xy basis is orthonormal");
        };
        let profile_id = document.allocate_sketch();
        document.insert_sketch(profile_id, "Profile".to_owned(), Sketch3::on_plane(basis));

        let feature = SweepFeature {
            direction: bone_kernel::SweepDirection::Bidirectional,
            guides: vec![SweepPath::Sketch(path_id)],
            ..SweepFeature::boss(profile_id, SweepPath::Sketch(path_id))
        };
        let sweep_id = document.commit_sweep(feature.clone());

        let Ok(()) = save(&document, &folder) else {
            panic!("save");
        };
        assert!(
            folder.sweep_path(sweep_id).exists(),
            "the sweep is written to disk"
        );

        let Ok(loaded) = load(&folder) else {
            panic!("load");
        };
        let Some(restored) = loaded.sweep(sweep_id) else {
            panic!("the sweep survives the round trip");
        };
        assert_eq!(
            restored, &feature,
            "the restored sweep matches what was saved"
        );

        let Some(sweep_feature) = loaded.feature_tree().feature_of_sweep(sweep_id) else {
            panic!("the sweep feature node survives the round trip");
        };
        assert!(
            loaded
                .feature_tree()
                .parents(sweep_feature)
                .contains(&path_feature),
            "the path dependency edge is reconstructed on load",
        );
    }

    #[test]
    fn ensure_ordered_rejects_a_child_before_its_parent() {
        use super::{FolderErrorKind, ensure_ordered};
        use crate::document::Document;

        let mut header = DocumentHeader::new(document_id(1), "order".to_owned());
        let sketch = SketchId::from(KeyData::from_ffi((1u64 << 32) | 1));
        let sketch_feature = header.feature_tree.push_sketch(sketch);
        let extrude = extrude_id(1);
        let extrude_feature = header.feature_tree.push_extrude(extrude, &blind(sketch));
        header.extrudes.insert(extrude, blind(sketch));
        header
            .feature_tree
            .move_before(extrude_feature, sketch_feature);

        let doc = Document::from_parts(header, BTreeMap::new(), BTreeMap::new());
        let Err(err) = ensure_ordered(doc.feature_tree()) else {
            panic!("a child ordered before its parent must be rejected");
        };
        assert!(matches!(
            err.into_kind(),
            FolderErrorKind::FeatureOrderViolation { parent, child }
                if parent == sketch_feature && child == extrude_feature
        ));
    }
}
