markhor_core 0.1.0-alpha.0.2

Core library for Markhor, a project connecting AI models, documents, and workflows for knowledge work
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
use crate::storage::{ConflictError, Error, Result};
use crate::storage::metadata::DocumentMetadata;
use crate::storage::ContentFile;
use regex::Regex;
use tokio::io::{AsyncRead, AsyncWriteExt};
use tokio::sync::MutexGuard;
use uuid::Uuid;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::fs::{self, OpenOptions};
use tracing::{debug, instrument, warn};

use super::workspace::NUM_METADATA_FILE_LOCKS;
use super::Workspace;

const MARKHOR_EXTENSION: &str = "markhor";



/// Represents a Markhor document, defined by a `.markhor` metadata file
/// and consisting of associated files in the same directory.
#[derive(Debug, Clone)]
pub struct Document {
    /// Absolute path to the .markhor file
    pub(crate) absolute_path: PathBuf,

    /// Unique ID
    pub id: Uuid,

    /// Workspace owning this document
    workspace: Arc<Workspace>,
    metadata: DocumentMetadata,
}

impl Document {
    /// Opens an existing document by reading its `.markhor` file.
    ///
    /// Checks if the file exists and is accessible.
    #[instrument(skip(absolute_path), fields(path = %absolute_path.display()))]
    pub(crate) async fn open(absolute_path: PathBuf, workspace: Arc<Workspace>) -> Result<Self> {
        validate_markhor_path(&absolute_path)?;

        // Ensure the file exists and we can read it (basic check)
        // `read_metadata_internal` will perform the actual read.
        if !fs::try_exists(&absolute_path).await.map_err(Error::Io)? {
            return Err(Error::FileNotFound(absolute_path));
        }
        if !fs::metadata(&absolute_path).await.map_err(Error::Io)?.is_file() {
            return Err(Error::InvalidPath(format!("Path is not a file: {}", absolute_path.display())));
        }

        // Try reading metadata to confirm it's a valid document structure
        let metadata = Self::read_metadata_internal(&absolute_path).await?;
        let id = metadata.id;

        debug!("Document opened successfully");
        Ok(Document { absolute_path, id, workspace, metadata })
    }

    /// Creates a new document with a `.markhor` file at the specified path.
    ///
    /// Performs conflict checks to ensure the new document doesn't clash
    /// with existing files or documents in the target directory according
    /// to the defined ambiguity rules.
    #[instrument(skip(absolute_path), fields(path = %absolute_path.display()))]
    pub(crate) async fn create(absolute_path: PathBuf, workspace: Arc<Workspace>) -> Result<Self> {
        validate_markhor_path(&absolute_path)?;
        let (dir, basename) = get_dir_and_basename(&absolute_path)?;

        // --- Conflict Detection ---
        check_for_conflicts(&dir, &basename).await?;
        // --- End Conflict Detection ---

        debug!("Conflict check passed. Creating new document.");
        let metadata = DocumentMetadata::new();
        let id = metadata.id;
        let content = serde_json::to_string_pretty(&metadata)?;

        fs::write(&absolute_path, content)
            .await
            .map_err(Error::Io)?;

        debug!("Document metadata file created successfully.");
        Ok(Document { absolute_path, id, workspace, metadata })
    }

    /// Returns the relative path to the document's `.markhor` file within the workspace.
    pub fn path(&self) -> &Path {
        &self.absolute_path.strip_prefix(&self.workspace.absolute_path)
            .expect("Internal error: Document is not in workspace")
    }

    pub fn name(&self) -> &str {
        self.absolute_path.file_stem()
            .and_then(OsStr::to_str).unwrap()
    }

    pub fn id(&self) -> &Uuid {
        &self.metadata.id
    }

    /// Reads and deserializes the document's metadata from its `.markhor` file.
    #[instrument(skip(self))]
    pub async fn read_metadata(&self) -> Result<DocumentMetadata> {
        let _lock = self.lock_metadata_file().await;
        Self::read_metadata_internal(&self.absolute_path).await
    }

    /// Reads and potentially updates document's metadata.
    #[instrument(skip(self, f))]
    pub(crate) async fn with_metadata<F, T>(&self, f: F) -> Result<T>
    where
        F: AsyncFnOnce(&mut Cow<DocumentMetadata>) -> T,
    {
        // Read metadata
        let _lock = self.lock_metadata_file().await;
        let metadata = Self::read_metadata_internal(&self.absolute_path).await?;

        // Pass borrow of metadata to callback
        let mut cow = Cow::Borrowed(&metadata);
        let value = f(&mut cow).await;

        // If metadata was mutated, update file
        if let Cow::Owned(updated_metadata) = cow {
            debug!("Saving metadata to {}", self.absolute_path.display());
            let content = serde_json::to_string_pretty(&updated_metadata)?;
            fs::write(&self.absolute_path, content)
                .await
                .map_err(Error::Io)?;
            debug!("Metadata saved successfully.");
        };

        // Return the result
        Ok(value)
    }

    /// Internal helper for reading metadata
    async fn read_metadata_internal(path: &Path) -> Result<DocumentMetadata> {
        debug!("Reading metadata from {}", path.display());
        let content = fs::read(path)
            .await
            .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                Error::FileNotFound(path.to_path_buf())
            } else {
                Error::Io(e)
            }
            })?;
        let metadata: DocumentMetadata = serde_json::from_slice(&content)?;
        Ok(metadata)
    }

    #[instrument(skip(self))]
    async fn lock_metadata_file(&self) -> MutexGuard<()> {
        let index = usize::try_from(self.id.as_fields().0).unwrap() % NUM_METADATA_FILE_LOCKS;
        self.workspace.metadata_file_locks[index].lock().await
    }

    /// Moves the document (including its `.markhor` file and all associated files)
    /// to a new location and/or gives it a new basename.
    ///
    /// The `new_markhor_path` must end in `.markhor`.
    /// Performs conflict checks in the destination directory before moving.
    /// Returns an updated `Document` instance pointing to the new location.
    #[instrument(skip(self), fields(new_path = %new_markhor_path.display()))]
    pub async fn move_to(mut self, new_markhor_path: PathBuf) -> Result<Self> {
        validate_markhor_path(&new_markhor_path)?;
        let (_old_dir, old_basename) = get_dir_and_basename(&self.absolute_path)?;
        let (new_dir, new_basename) = get_dir_and_basename(&new_markhor_path)?;

        if self.absolute_path == new_markhor_path {
            debug!("Move target is the same as current path, no action needed.");
            return Ok(self); // No-op
        }

        // --- Conflict Detection in Destination ---
        // Important: Check for conflicts *before* starting the move.
        // Skip check if the file being moved *is* the potential conflict
        // (e.g., moving doc.markhor to doc.markhor in the same dir - already handled)
        check_for_conflicts(&new_dir, &new_basename).await?;
        // --- End Conflict Detection ---

        debug!("Conflict check passed. Proceeding with move.");
        let files_to_move = self.list_all_associated_files().await?;

        // Use a staging approach? For simplicity now, move directly.
        // Note: This is NOT atomic across all files. If one rename fails,
        // the document might be in an inconsistent state.
        // A more robust implementation might move to a temp dir first.

        for old_file_path in files_to_move {
             let file_name = old_file_path
                 .file_name()
                 .ok_or_else(|| Error::InvalidPath(format!("Cannot get filename for {}", old_file_path.display())))?;

             let new_file_path = if old_file_path == self.absolute_path {
                 new_markhor_path.clone() // Handle the .markhor file itself
             } else {
                 // Construct new path based on new basename and original extension/suffix part
                 let original_filename = file_name.to_string_lossy();
                 let suffix_part = original_filename
                     .strip_prefix(&old_basename)
                     .ok_or_else(|| Error::InvalidPath(format!("File {} does not match basename {}", original_filename, old_basename)))?;

                new_dir.join(format!("{}{}", new_basename, suffix_part))
             };

             debug!("Moving {} -> {}", old_file_path.display(), new_file_path.display());
             fs::rename(&old_file_path, &new_file_path)
                .await
                .map_err(|e| {
                    warn!("Failed to move file {} to {}: {}. Document may be in inconsistent state.", old_file_path.display(), new_file_path.display(), e);
                    Error::Io(e)
                })?;
        }

        // Update the document's path internally
        self.absolute_path = new_markhor_path;
        debug!("Move operation completed.");
        Ok(self)
    }


    /// Deletes the document's `.markhor` file and all associated files.
    ///
    /// This operation is potentially destructive and irreversible.
    #[instrument(skip(self))]
    pub async fn delete(self) -> Result<()> {
        debug!("Attempting to delete document: {}", self.absolute_path.display());
        let files_to_delete = self.list_all_associated_files().await?;

        let mut errors = Vec::new();
        for file_path in files_to_delete {
            debug!("Deleting file: {}", file_path.display());
            if let Err(e) = fs::remove_file(&file_path).await {
                // Log error but continue trying to delete others
                warn!("Failed to delete file {}: {}", file_path.display(), e);
                if e.kind() != std::io::ErrorKind::NotFound { // Don't error if already gone
                   errors.push(Error::Io(e));
                }
            }
        }

        if let Some(first_error) = errors.into_iter().next() {
             Err(first_error) // Return the first error encountered
        } else {
             debug!("Document deleted successfully.");
             Ok(())
        }
    }

    pub async fn file(&self, file_name: &str) -> Result<ContentFile> {
        // TODO: No need to iterate all files if we know the name
        let files = self.list_content_files_internal(None).await?;
        files.into_iter()
            .find(|file| file.file_name() == file_name)
            .ok_or_else(|| Error::FileNotFound(self.absolute_path.with_file_name(file_name)))
    }

    /// Returns a list of all files associated with this document
    /// (excluding the `.markhor` file itself).
    #[instrument(skip(self))]
    pub async fn files(&self) -> Result<Vec<ContentFile>> {
        self.list_content_files_internal(None).await
    }

    /// Returns a list of associated files filtered by a specific extension.
    /// The extension should be provided *without* the leading dot (e.g., "pdf", "txt").
    #[instrument(skip(self))]
    pub async fn files_by_extension(&self, extension: &str) -> Result<Vec<ContentFile>> {
        self.list_content_files_internal(Some(extension)).await
    }

    /// Returns a list of files representing the primary content of this document.
    /// 
    /// The current implementation simply returns all Markdown (.md) files, but this behavior will
    /// be refined in the future.
    pub async fn primary_content_files(&self) -> Result<Vec<ContentFile>> {
        self.list_content_files_internal(Some("md")).await
    }

    /// Adds a new file to the document with the specified extension.
    /// 
    /// If a file with the same name already exists, a hexadecimal suffix will be added.
    pub async fn add_file<R: AsyncRead + Unpin + ?Sized>(&self, extension: &str, content: &mut R) -> Result<ContentFile> {
        let mut result = self.add_file_internal(extension, None, content).await;
        let mut suffix = 1u32;
        while let Err(err) = result {
            if let Error::Io(io_err) = err {
                if io_err.kind() == std::io::ErrorKind::AlreadyExists {
                    // File already exists, try with a new suffix
                    result = self.add_file_internal(extension, Some(suffix), content).await;
                    suffix += 1;
                } else {
                    return Err(Error::Io(io_err)); // Some other IO error occurred
                }
            } else {
                return Err(err); // Non-IO error occurred
            }
            // Sanity check
            if suffix > 100 {
                return Err(Error::ContentFileNotCreated(String::from("Too many suffixes")));
            }
        }
        return result;
    }

    /// Adds a new file to the document with the specified extension and hexadecimal suffix.
    /// 
    /// If a file with the same name already exists, it will fail.
    pub async fn add_file_with_suffix<R: AsyncRead + Unpin + ?Sized>(&self, extension: &str, suffix: u32, content: &mut R) -> Result<ContentFile> {
        self.add_file_internal(extension, Some(suffix), content).await
    }

    async fn add_file_internal<R: AsyncRead + Unpin + ?Sized>(&self, extension: &str, suffix: Option<u32>, content: &mut R) -> Result<ContentFile> {
        let (dir, basename) = get_dir_and_basename(&self.absolute_path)?;
        let new_file_name = match suffix {
            Some(sfx) => format!("{}.{:x}.{}", basename, sfx, extension),
            None => format!("{}.{}", basename, extension),
        };
        let new_file_path = dir.join(new_file_name);

        // Create the file
        //let file = fs::File::create(&new_file_path).await.map_err(Error::Io)?;
        let file = OpenOptions::new()
            .write(true)
            .create_new(true) // Fail if the file already exists
            .open(&new_file_path)
            .await
            .map_err(Error::Io)?;

        // Write the content to the file
        let mut writer = tokio::io::BufWriter::new(file);
        tokio::io::copy(content, &mut writer).await.map_err(Error::Io)?;
        writer.flush().await.map_err(Error::Io)?;

        // Return a ContentFile instance for the new file
        Ok(ContentFile::new(new_file_path, self))
    }


    // --- Internal Helpers ---

    /// Lists ContentFile instances, optionally filtering by extension.
    async fn list_content_files_internal(&self, extension_filter: Option<&str>) -> Result<Vec<ContentFile>> {
        let (dir, basename) = get_dir_and_basename(&self.absolute_path)?;
        let mut files = Vec::new();
        let mut read_dir = fs::read_dir(&dir).await.map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                Error::DirectoryNotFound(dir.clone())
            } else {
                Error::Io(e)
            }
        })?;

        while let Some(entry) = read_dir.next_entry().await.map_err(Error::Io)? {
            let path = entry.path();
            if path.is_file() {
                // Skip the .markhor file itself
                if path == self.absolute_path {
                    continue;
                }

                if let Some(file_name) = path.file_name().and_then(OsStr::to_str) {
                     if is_potential_content_file(file_name, &basename) {
                        // Apply extension filter if provided
                        if let Some(filter_ext) = extension_filter {
                            if path.extension().and_then(OsStr::to_str) == Some(filter_ext) {
                                files.push(ContentFile::new(path, self));
                            }
                        } else {
                            // No filter, add the file
                            files.push(ContentFile::new(path, self));
                        }
                    }
                }
            }
        }
        Ok(files)
    }

    /// Lists all files belonging to the document, *including* the .markhor file.
    /// Used internally for move/delete operations.
    async fn list_all_associated_files(&self) -> Result<Vec<PathBuf>> {
         let (dir, basename) = get_dir_and_basename(&self.absolute_path)?;
         let mut paths = vec![self.absolute_path.clone()]; // Start with the metadata file
         let mut read_dir = fs::read_dir(dir).await.map_err(Error::Io)?;

         while let Some(entry) = read_dir.next_entry().await.map_err(Error::Io)? {
            let path = entry.path();
            if path.is_file() && path != self.absolute_path { // Exclude markhor here
                 if let Some(file_name) = path.file_name().and_then(OsStr::to_str) {
                     if is_potential_content_file(file_name, &basename) {
                        paths.push(path);
                    }
                }
            }
        }
        Ok(paths)
    }
}

impl PartialEq for Document {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Document {}

impl Hash for Document {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}


// --- Standalone Helper Functions ---

/// Validates that a path points to a potential `.markhor` file.
fn validate_markhor_path(path: &Path) -> Result<()> {
    if path.extension().and_then(OsStr::to_str) != Some(MARKHOR_EXTENSION) {
        return Err(Error::NotMarkhorFile(path.to_path_buf()));
    }
    if path.file_stem().is_none() {
        return Err(Error::NoFileStem(path.to_path_buf()));
    }
    if path.parent().is_none() {
        return Err(Error::NoParentDirectory(path.to_path_buf()));
    }
    Ok(())
}

/// Extracts the parent directory and base filename (stem) from a path.
fn get_dir_and_basename(path: &Path) -> Result<(PathBuf, String)> {
    let dir = path.parent()
        .ok_or_else(|| Error::NoParentDirectory(path.to_path_buf()))?
        .to_path_buf();
    let basename = path.file_stem()
        .and_then(OsStr::to_str)
        .ok_or_else(|| Error::NoFileStem(path.to_path_buf()))?
        .to_string();
    Ok((dir, basename))
}

/// Parses a file stem into its "true base" and an optional hex suffix.
/// E.g., "doc.1a" -> ("doc", Some("1a")), "mydoc" -> ("mydoc", None)
fn parse_basename(stem: &str) -> Result<(String, Option<String>)> {
    // Use lazy_static or once_cell for better performance if called frequently
    let hex_suffix_re = Regex::new(r"^(.*)\.([0-9a-fA-F]+)$").unwrap(); // Handle potential regex error better in real code

    if let Some(captures) = hex_suffix_re.captures(stem) {
        // Check if the "base" part itself could be misinterpreted (e.g. "doc.1.2")
        // For now, assume the regex correctly finds the *last* hex part as the suffix.
        let true_base = captures.get(1).map_or("", |m| m.as_str()).to_string();
        let hex_suffix = captures.get(2).map_or("", |m| m.as_str()).to_string();

        if true_base.is_empty() {
            // Avoid case like ".a1f.markhor" being parsed incorrectly
            Err(Error::BasenameParseError(stem.to_string()))
        } else {
            Ok((true_base, Some(hex_suffix)))
        }
    } else {
        // No hex suffix found
        Ok((stem.to_string(), None))
    }
}

/// Checks if a filename matches the pattern for belonging to a document
/// with the given basename (`basename.*` or `basename.{hex}.*`).
fn is_potential_content_file(filename: &str, doc_basename: &str) -> bool {
    if !filename.to_lowercase().starts_with(&doc_basename.to_lowercase()) {
        return false;
    }
    if filename.to_lowercase().ends_with(&format!(".{}", MARKHOR_EXTENSION)) {
        return false; // Exclude .markhor files themselves
    }

    let remainder = &filename[doc_basename.len()..];

    // Case 1: filename == doc_basename (should not happen for files other than .markhor?)
    // If it does, it doesn't fit the pattern with an extension.
    if remainder.is_empty() {
        return false;
    }

    // Case 2: Direct extension (e.g., "doc.txt")
    if remainder.starts_with('.') && !remainder.contains('/') && remainder.len() > 1 { // Ensure it has an extension part
         // Check if the part *after* the first dot looks like a hex suffix or not
         let mut parts = remainder[1..].splitn(2, '.');
         let first_part = parts.next().unwrap_or("");
         let second_part = parts.next(); // The actual extension if hex suffix exists

         if second_part.is_some() { // e.g., ".a1.txt"
              // Check if first_part is purely hex
              if !first_part.is_empty() && first_part.chars().all(|c| c.is_ascii_hexdigit()) {
                 return true; // Matches basename.{hex}.*
              } else {
                  // It's something like "basename.something.txt" where "something" is not hex
                  // This should *not* match if we interpret the rule strictly.
                  // Or should it? Let's assume NOT for now based on `basename.{hex}.*`
                  return false;
              }
         } else { // e.g., ".txt"
            // No second part means it's basename.ext
             return true;
         }
    }

    false
}


/// Performs conflict checks before creating or moving a document.
async fn check_for_conflicts(target_dir: &Path, target_basename: &str) -> Result<()> {
    debug!("Checking for conflicts for basename '{}' in directory '{}'", target_basename, target_dir.display());

    let target_markhor_path = target_dir.join(format!("{}.{}", target_basename, MARKHOR_EXTENSION));
    let (target_true_base, target_hex_suffix) = parse_basename(target_basename)?;

    let mut read_dir = match fs::read_dir(target_dir).await {
        Ok(rd) => rd,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            // Target directory doesn't exist, so no conflicts within it.
            // The `create` or `move_to` operation might create it later.
            return Ok(());
        }
        Err(e) => return Err(Error::Io(e)),
    };

    let mut existing_suffixed_documents = Vec::new();
    let mut existing_base_document_found = false;


    while let Some(entry) = read_dir.next_entry().await.map_err(Error::Io)? {
        let path = entry.path();
        let file_name_os = entry.file_name();
        let Some(file_name) = file_name_os.to_str() else { continue; }; // Skip non-unicode filenames

        if !path.is_file() {
            continue; // Skip directories and other non-file entries
        }

        // Rule 1 Check: Direct .markhor conflict
        if path == target_markhor_path {
             debug!("Conflict Rule 1 Triggered: Markhor file exists: {}", path.display());
             return Err(ConflictError::MarkhorFileExists(path).into());
        }

        // Rule 2 Check: Existing file would be adopted?
        // This check ensures *no* file would be implicitly "adopted" by the new document, 
        // potentially misrepresenting its origin.
        if is_potential_content_file(file_name, target_basename) {
            // Found a file (e.g., target_basename.txt or target_basename.a1.pdf)
            // that would match the *new* document's pattern.
            // This is disallowed to prevent accidental adoption.
            debug!("Conflict Rule 2 Triggered: Existing file {} would be adopted by {}", path.display(), target_basename);
            return Err(ConflictError::ExistingFileWouldBeAdopted(path).into());
        }

        // --- Gather info for ambiguity checks ---
        if file_name.ends_with(&format!(".{}", MARKHOR_EXTENSION)) {
            if let Some(stem) = path.file_stem().and_then(OsStr::to_str) {
                 match parse_basename(stem) {
                     Ok((true_base, Some(_))) => {
                        // This is an existing suffixed document
                        if true_base == target_true_base {
                            existing_suffixed_documents.push(stem.to_string());
                        }
                     }
                     Ok((true_base, None)) => {
                        // This is an existing base document
                        if true_base == target_true_base {
                            existing_base_document_found = true;
                        }
                     }
                     Err(_) => {
                         warn!("Could not parse basename for existing file: {}", path.display());
                     } // Ignore parse errors for existing files for now
                 }
             }
        }
        // --- End info gathering ---

    } // End while loop through directory entries

    // --- Ambiguity Checks (Rules 3 & 4) ---

    // Rule 3 Check: Suffix-Base Ambiguity
    // Is the target a suffixed doc (doc.4.markhor) AND does the base (doc.markhor) exist?
    if let Some(hex_suffix) = &target_hex_suffix {
        if existing_base_document_found {
             debug!("Conflict Rule 3 Triggered: Target '{}.{}.markhor' conflicts with existing base '{}.markhor'", target_true_base, hex_suffix, target_true_base);
             return Err(ConflictError::SuffixBaseAmbiguity(target_true_base.clone(), hex_suffix.clone()).into());
        }
    }

    // Rule 4 Check: Base-Suffix Ambiguity
    // Is the target a base doc (doc.markhor) AND does *any* suffixed version (doc.*.markhor) exist?
    if target_hex_suffix.is_none() && !existing_suffixed_documents.is_empty() {
         debug!("Conflict Rule 4 Triggered: Target '{}.markhor' conflicts with existing suffixed documents like '{}.markhor'", target_true_base, existing_suffixed_documents[0]);
         return Err(ConflictError::BaseSuffixAmbiguity(target_true_base.clone(), existing_suffixed_documents[0].clone()).into()); // Report first conflict
    }

    // --- End Ambiguity Checks ---

    debug!("No conflicts found.");
    Ok(())
}


// Example Usage (requires a tokio runtime)
#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    // Helper to create a dummy file
    async fn create_dummy_file(path: &Path) {
        fs::write(path, "").await.expect("Failed to create dummy file");
    }

    // #[tokio::test]
    // async fn test_create_and_open_document() {
    //     let dir = tempdir().unwrap();
    //     let doc_path = dir.path().join("mydoc.markhor");

    //     let doc = Document::create(doc_path.clone(), dir.path().to_path_buf()).await.unwrap();
    //     assert!(doc_path.exists());

    //     let metadata = doc.read_metadata().await.unwrap();
    //     println!("Created doc with UUID: {}", metadata.id);

    //     let opened_doc = Document::open(doc_path.clone(), dir.path().to_path_buf()).await.unwrap();
    //     let opened_metadata = opened_doc.read_metadata().await.unwrap();
    //     assert_eq!(metadata.id, opened_metadata.id);

    //     opened_doc.delete().await.unwrap();
    //     assert!(!doc_path.exists());
    // }

    //  #[tokio::test]
    // async fn test_list_files() {
    //     let dir = tempdir().unwrap();
    //     let doc_path = dir.path().join("testdoc.markhor");
    //     let pdf_path = dir.path().join("testdoc.pdf");
    //     let txt_path = dir.path().join("testdoc.txt");
    //     let hex_txt_path = dir.path().join("testdoc.a1f.txt");
    //     let unrelated_path = dir.path().join("other.txt");
    //     let unrelated_hex_path = dir.path().join("testdoc_extra.txt"); // Doesn't match pattern

    //     let doc = Document::create(doc_path.clone(), dir.path().to_path_buf()).await.unwrap();
    //     create_dummy_file(&pdf_path).await;
    //     create_dummy_file(&txt_path).await;
    //     create_dummy_file(&hex_txt_path).await;
    //     create_dummy_file(&unrelated_path).await;
    //     create_dummy_file(&unrelated_hex_path).await;

    //     let files = doc.files().await.unwrap();
    //     assert_eq!(files.len(), 3);
    //     let paths: Vec<_> = files.iter().map(|f| f.path().to_path_buf()).collect();
    //     assert!(paths.contains(&pdf_path));
    //     assert!(paths.contains(&txt_path));
    //     assert!(paths.contains(&hex_txt_path));

    //     let txt_files = doc.files_by_extension("txt").await.unwrap();
    //     assert_eq!(txt_files.len(), 2);
    //      let txt_paths: Vec<_> = txt_files.iter().map(|f| f.path().to_path_buf()).collect();
    //     assert!(txt_paths.contains(&txt_path));
    //     assert!(txt_paths.contains(&hex_txt_path));

    //     doc.delete().await.unwrap();
    //     assert!(!doc_path.exists());
    //     assert!(!pdf_path.exists());
    //     assert!(!txt_path.exists());
    //     assert!(!hex_txt_path.exists());
    //     // Unrelated files should remain
    //     assert!(unrelated_path.exists());
    //      assert!(unrelated_hex_path.exists());
    // }

    // #[tokio::test]
    // async fn test_conflict_rule1_markhor_exists() {
    //     let dir = tempdir().unwrap();
    //     let doc_path = dir.path().join("conflict1.markhor");
    //     create_dummy_file(&doc_path).await; // Pre-create the file

    //     let result = Document::create(doc_path.clone(), dir.path().to_path_buf()).await;
    //     assert!(matches!(result, Err(Error::Conflict(ConflictError::MarkhorFileExists(_)))));
    // }

    //  #[tokio::test]
    // async fn test_conflict_rule2_file_would_be_adopted() {
    //     let dir = tempdir().unwrap();
    //     let doc_path = dir.path().join("conflict2.markhor");
    //     let existing_file = dir.path().join("conflict2.txt"); // Would be adopted
    //     create_dummy_file(&existing_file).await;

    //     let result = Document::create(doc_path.clone(), dir.path().to_path_buf()).await;
    //     assert!(matches!(result, Err(Error::Conflict(ConflictError::ExistingFileWouldBeAdopted(_)))));

    //     let existing_hex_file = dir.path().join("conflict2.a1.pdf"); // Would also be adopted
    //     create_dummy_file(&existing_hex_file).await;
    //     let result2 = Document::create(doc_path.clone(), dir.path().to_path_buf()).await;
    //      assert!(matches!(result2, Err(Error::Conflict(ConflictError::ExistingFileWouldBeAdopted(_)))));
    // }

    // #[tokio::test]
    // async fn test_conflict_rule3_suffix_base_ambiguity() {
    //     let dir = tempdir().unwrap();
    //     let base_doc_path = dir.path().join("conflict3.markhor");
    //     let suffix_doc_path = dir.path().join("conflict3.4a.markhor");

    //     // Create the base document first
    //     Document::create(base_doc_path.clone(), dir.path().to_path_buf()).await.unwrap();

    //     // Now try to create the suffixed one - should conflict
    //     let result = Document::create(suffix_doc_path.clone(), dir.path().to_path_buf()).await;
    //      println!("{:?}", result); // Debug print
    //     assert!(matches!(result, Err(Error::Conflict(ConflictError::SuffixBaseAmbiguity(b,s))) if b == "conflict3" && s == "4a"));
    // }

    //  #[tokio::test]
    // async fn test_conflict_rule4_base_suffix_ambiguity() {
    //     let dir = tempdir().unwrap();
    //     let base_doc_path = dir.path().join("conflict4.markhor");
    //     let suffix_doc_path = dir.path().join("conflict4.4a.markhor");

    //     // Create the suffixed document first
    //     Document::create(suffix_doc_path.clone(), dir.path().to_path_buf()).await.unwrap();

    //     // Now try to create the base one - should conflict
    //     let result = Document::create(base_doc_path.clone(), dir.path().to_path_buf()).await;
    //      println!("{:?}", result); // Debug print
    //     assert!(matches!(result, Err(Error::Conflict(ConflictError::BaseSuffixAmbiguity(b,s))) if b == "conflict4" && s == "conflict4.4a"));
    // }

    // #[tokio::test]
    // async fn test_move_document() {
    //     let dir = tempdir().unwrap();
    //     let old_doc_path = dir.path().join("move_me.markhor");
    //     let old_file_path = dir.path().join("move_me.data");
    //     let new_doc_path = dir.path().join("subdir/moved_doc.markhor");

    //     // Create target subdir
    //     fs::create_dir(dir.path().join("subdir")).await.unwrap();

    //     let doc = Document::create(old_doc_path.clone(), dir.path().to_path_buf()).await.unwrap();
    //     create_dummy_file(&old_file_path).await;

    //     assert!(old_doc_path.exists());
    //     assert!(old_file_path.exists());

    //     let moved_doc = doc.move_to(new_doc_path.clone()).await.unwrap();

    //     assert!(!old_doc_path.exists());
    //     assert!(!old_file_path.exists());
    //     assert!(new_doc_path.exists());
    //     assert!(dir.path().join("subdir/moved_doc.data").exists()); // Check associated file moved correctly

    //     // Check internal path updated
    //     assert_eq!(moved_doc.absolute_path, new_doc_path);

    //     // Clean up
    //     moved_doc.delete().await.unwrap();
    //      assert!(!new_doc_path.exists());
    //     assert!(!dir.path().join("subdir/moved_doc.data").exists());
    // }

    //  #[tokio::test]
    // async fn test_move_conflict() {
    //     let dir = tempdir().unwrap();
    //     let doc1_path = dir.path().join("doc1.markhor");
    //     let doc2_path = dir.path().join("doc2.markhor");
    //     let conflicting_file = dir.path().join("doc1.txt"); // Will conflict if doc2 moves to doc1

    //     let doc1 = Document::create(doc1_path.clone(), dir.path().to_path_buf()).await.unwrap();
    //     let doc2 = Document::create(doc2_path.clone(), dir.path().to_path_buf()).await.unwrap();
    //     create_dummy_file(&conflicting_file).await; // Create file potentially owned by doc1

    //      // Try moving doc2 to doc1 -> Conflict Rule 1 (MarkhorFileExists) takes precedence here
    //     let move_result = doc2.move_to(doc1_path.clone()).await;
    //     assert!(matches!(move_result, Err(Error::Conflict(ConflictError::MarkhorFileExists(p))) if p == doc1_path));

    //     // Need to reload doc2 as it was consumed by the failed move attempt
    //     let doc2_reloaded = Document::open(doc2_path, dir.path().to_path_buf()).await.unwrap();
    //     doc1.delete().await.unwrap();
    //     doc2_reloaded.delete().await.unwrap();
    // }

    #[tokio::test]
    async fn test_parse_basename_logic() {
        assert_eq!(parse_basename("doc").unwrap(), ("doc".to_string(), None));
        assert_eq!(parse_basename("doc.txt").unwrap(), ("doc.txt".to_string(), None));
        assert_eq!(parse_basename("doc.1a").unwrap(), ("doc".to_string(), Some("1a".to_string())));
        assert_eq!(parse_basename("my.doc.with.dots.f0f").unwrap(), ("my.doc.with.dots".to_string(), Some("f0f".to_string())));
        assert_eq!(parse_basename("nodigits.a").unwrap(), ("nodigits".to_string(), Some("a".to_string()))); // "a" is hex, regex matches
        assert!(parse_basename(".abc").is_err()); // Invalid starting dot
        assert_eq!(parse_basename("doc.1").unwrap(), ("doc".to_string(), Some("1".to_string())));
        assert_eq!(parse_basename("doc.1.2").unwrap(), ("doc.1".to_string(), Some("2".to_string()))); // Regex matches last part
    }

     #[tokio::test]
    async fn test_is_potential_content_file_logic() {
        // Base doc: "mydoc"
        assert!(is_potential_content_file("mydoc.txt", "mydoc"));
        assert!(is_potential_content_file("mydoc.pdf", "mydoc"));
        assert!(is_potential_content_file("mydoc.a1.txt", "mydoc"));
        assert!(is_potential_content_file("mydoc.00ff.dat", "mydoc"));
        assert!(!is_potential_content_file("mydoc_extra.txt", "mydoc"));
        assert!(!is_potential_content_file("otherdoc.txt", "mydoc"));
        assert!(is_potential_content_file("mydoc.a1", "mydoc")); // "a1" is extension, not hex
        assert!(!is_potential_content_file("mydoc", "mydoc")); // No extension
        assert!(!is_potential_content_file("mydoc.markhor", "mydoc")); // Usually handled separately
        assert!(!is_potential_content_file("mydoc.v1.txt", "mydoc")); // "v1" is not hex

        // Should be case-insensitive
        assert!(is_potential_content_file("mydoc.pdf", "MYDOC"));
        assert!(is_potential_content_file("mydoc.a1.txt", "MYDOC"));
        assert!(is_potential_content_file("mydoc.A1.txt", "MYDOC"));
        assert!(is_potential_content_file("MYDOC.00ff.dat", "mydoc"));
        assert!(is_potential_content_file("MYDOC.a1", "mydoc"));
        assert!(is_potential_content_file("mydoc.A1", "mydoc"));

        // Suffixed doc: "report.v1.a0"
        let basename = "report.v1.a0";
        assert!(is_potential_content_file("report.v1.a0.csv", basename));
        assert!(is_potential_content_file("report.v1.a0.b1.json", basename)); // report.v1.a0.{hex}.json
        assert!(!is_potential_content_file("report.v1.a0", basename));
        assert!(!is_potential_content_file("report.v1.a0txt", basename)); // Needs dot separator
        assert!(!is_potential_content_file("report.v1.csv", basename)); // Belongs to report.v1 potentially
        assert!(!is_potential_content_file("report.v1.a0.markhor", basename));

    }
}