use crate::repository::{data::DataRepository, DoqiumClient};
use anyhow::Result;

pub async fn list_notes(token: &str, notebook_id: &str) -> Result<Vec<crate::api::Note>> {
    let client = DoqiumClient::new(token)?;
    let repo = DataRepository::new(&client);
    let notes = repo.list_notes(notebook_id).await?;
    Ok(notes.into_iter().map(|n| n.into()).collect())
}

pub async fn get_note(token: &str, notebook_id: &str, note_id: &str) -> Result<crate::api::Note> {
    let client = DoqiumClient::new(token)?;
    let repo = DataRepository::new(&client);
    let note = repo.get_note(notebook_id, note_id).await?;
    Ok(note.into())
}

pub async fn create_note(
    token: &str,
    notebook_id: &str,
    note_request: &serde_json::Value,
) -> Result<String> {
    let client = DoqiumClient::new(token)?;
    let repo = DataRepository::new(&client);
    let note_req: crate::doqium_data_client::models::NoteRequest =
        serde_json::from_value(note_request.clone())?;
    repo.create_note(notebook_id, note_req).await
}

pub async fn update_note(
    token: &str,
    notebook_id: &str,
    note_id: &str,
    note_request: &serde_json::Value,
) -> Result<()> {
    let client = DoqiumClient::new(token)?;
    let repo = DataRepository::new(&client);
    let note_req: crate::doqium_data_client::models::NoteRequest =
        serde_json::from_value(note_request.clone())?;
    repo.update_note(notebook_id, note_id, note_req).await
}

pub async fn delete_note(token: &str, notebook_id: &str, note_id: &str) -> Result<()> {
    let client = DoqiumClient::new(token)?;
    let repo = DataRepository::new(&client);
    repo.delete_note(notebook_id, note_id).await
}
