//! Tauri commands for PDF attachment and retrieval.

use crate::state::AppState;
use brittle_core::{PdfAttachment, ReferenceId};
use std::path::PathBuf;
use tauri::State;

/// Attach a PDF file to a reference by copying it into the repository.
///
/// `source_path` is the absolute path to the file to copy.
/// Returns the stored attachment metadata.
#[tauri::command]
pub fn attach_pdf(
    state: State<AppState>,
    reference_id: ReferenceId,
    source_path: String,
) -> Result<PdfAttachment, String> {
    let path = PathBuf::from(&source_path);
    state.with_repo(|b| b.attach_pdf(reference_id, &path))
}

/// Return the absolute filesystem path to the PDF attached to a reference.
///
/// Returns an error if no PDF is attached.
#[tauri::command]
pub fn get_pdf_path(state: State<AppState>, reference_id: ReferenceId) -> Result<String, String> {
    state.with_repo_read(|b| {
        b.get_pdf_path(reference_id)
            .map(|p| p.to_string_lossy().into_owned())
    })
}
