//! CUT (Scene Cut Detection) processing module

use anyhow::Result;
use momentry_core::ui::progress::{ProcessorType, ProgressState, ProgressUi};
use momentry_core::OutputDir;
use std::path::Path;
use std::sync::{Arc, Mutex};

/// Process CUT module
pub async fn process_cut_module(
    cut_path: &Path,
    video_path: &str,
    uuid: &str,
    progress_state: &Arc<Mutex<ProgressState>>,
    ui: &Arc<Mutex<Option<ProgressUi>>>,
) -> Result<()> {
    {
        let mut state = progress_state.lock().unwrap();
        state.get_processor(ProcessorType::Cut).start(1);
    }

    let cut_result = momentry_core::core::processor::process_cut(
        video_path,
        cut_path.to_str().unwrap(),
        Some(uuid),
    )
    .await?;

    let cut_json = serde_json::to_string_pretty(&cut_result)?;
    std::fs::write(cut_path, &cut_json)?;

    let output_dir = OutputDir::new();
    let _ = output_dir.backup_file(uuid, "cut.json");

    println!("  ✓ CUT saved: {} scenes", cut_result.scenes.len());

    {
        let mut state = progress_state.lock().unwrap();
        state
            .get_processor(ProcessorType::Cut)
            .complete(&format!("{} scenes", cut_result.scenes.len()));
    }

    if let Some(ref mut ui) = *ui.lock().unwrap() {
        let _ = ui.render();
    }

    Ok(())
}
