// use std::sync::Arc;

// use barber::{ProgressBar, ProgressRenderer};
// use lunar_lib::database::{DatabaseError, Db, Entry};
// use thiserror::Error;

// use crate::{
//     database::Library,
//     library::{hash_source_files, track::Track},
// };

// #[derive(Debug, Error)]
// pub enum OrphanRelinkError {
//     #[error("IoError: {0}")]
//     Io(#[from] std::io::Error),

//     #[error("Database Error: {0}")]
//     Database(#[from] DatabaseError),
// }

// pub fn relink_orphans(
//     progress_renderer: Arc<dyn ProgressRenderer>,
// ) -> Result<(), OrphanRelinkError> {
//     let mut check_tracks = {
//         let db = Db::<Library>::open()?;
//         Entry::<Track>::db_get_all(&db).map_err(DatabaseError::Transaction)?
//     };

//     if check_tracks.is_empty() {
//         return Ok(());
//     }

//     let sources = hash_source_files()?;

//     check_tracks.retain(|t| {
//         let expected_source = t.container().path();
//         !expected_source.exists() || sources.get(&t.id()).is_some_and(|t| t != expected_source)
//     });

//     let progress_bar = ProgressBar::new(0, check_tracks.len(), progress_renderer);
//     progress_bar.set_label("Relinking orphaned sources...");
//     progress_bar.update();

//     for mut track in check_tracks {
//         let db = Db::<Library>::open()?;
//         if let Some(source) = sources.get(&track.id()) {
//             track.set_source_file(source.to_owned());

//             let title = track.metadata().safe_title().to_owned();
//             track.db_upsert(db).map_err(DatabaseError::Transaction)?;

//             progress_bar.set_label(&format!("Relinked {title} to {}", source.display()));
//         } else {
//             let title = track.metadata().safe_title().to_owned();

//             track
//                 .id()
//                 .db_delete(db)
//                 .map_err(DatabaseError::Transaction)?;

//             progress_bar.set_label(&format!(
//                 "Failed to relink {title}: No source found. Removing from library",
//             ));
//         }

//         progress_bar.increment();
//     }
//     progress_bar.flush();
//     Ok(())
// }
