use crate::research::{
    ArchiveResearchNoteRequest, ArchiveResearchWorkspaceRequest, CaptureResearchNoteRequest,
    CreateResearchWorkspaceRequest, ExportResearchBundleRequest, GetNoteAuditTrailRequest,
    GetNotesByTickerRequest, GetWorkspaceProjectionRequest, GhostNote, ListNoteLinksRequest,
    ListResearchJobsRequest, ListResearchNotesRequest, ListWorkspaceGhostNotesRequest,
    NoteAuditTrail, NoteCaptureResult, NoteLink, PipelineJob, PreviewLinkRequest,
    PreviewLinkResult, PromoteNoteToThesisRequest, ResearchBundleExport, ResearchNote,
    ResearchWorkspace, RetryResearchJobsRequest, ReviewGhostNoteRequest,
    SearchResearchNotesRequest, UpdateResearchNoteRequest, UpdateResearchWorkspaceRequest,
    WorkspaceProjection,
};
use crate::state::AppState;

#[tauri::command]
pub async fn create_research_workspace(
    state: tauri::State<'_, AppState>,
    request: CreateResearchWorkspaceRequest,
) -> Result<ResearchWorkspace, String> {
    state
        .research_service
        .create_workspace(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn list_research_workspaces(
    state: tauri::State<'_, AppState>,
) -> Result<Vec<ResearchWorkspace>, String> {
    state
        .research_service
        .list_workspaces()
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn get_research_workspace(
    state: tauri::State<'_, AppState>,
    workspace_id: String,
) -> Result<ResearchWorkspace, String> {
    state
        .research_service
        .get_workspace(&workspace_id)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn capture_research_note(
    state: tauri::State<'_, AppState>,
    request: CaptureResearchNoteRequest,
) -> Result<NoteCaptureResult, String> {
    state
        .research_service
        .capture_note(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn preview_note_link(
    state: tauri::State<'_, AppState>,
    request: PreviewLinkRequest,
) -> Result<PreviewLinkResult, String> {
    state
        .research_service
        .preview_note_link(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn update_research_note(
    state: tauri::State<'_, AppState>,
    request: UpdateResearchNoteRequest,
) -> Result<ResearchNote, String> {
    state
        .research_service
        .update_note(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn archive_research_note(
    state: tauri::State<'_, AppState>,
    request: ArchiveResearchNoteRequest,
) -> Result<ResearchNote, String> {
    state
        .research_service
        .archive_note(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn list_research_notes(
    state: tauri::State<'_, AppState>,
    request: ListResearchNotesRequest,
) -> Result<Vec<ResearchNote>, String> {
    state
        .research_service
        .list_notes(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn get_notes_by_ticker(
    state: tauri::State<'_, AppState>,
    request: GetNotesByTickerRequest,
) -> Result<Vec<ResearchNote>, String> {
    state
        .research_service
        .get_notes_by_ticker(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn get_workspace_projection(
    state: tauri::State<'_, AppState>,
    request: GetWorkspaceProjectionRequest,
) -> Result<WorkspaceProjection, String> {
    state
        .research_service
        .get_workspace_projection(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn list_note_links(
    state: tauri::State<'_, AppState>,
    request: ListNoteLinksRequest,
) -> Result<Vec<NoteLink>, String> {
    state
        .research_service
        .list_note_links(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn list_workspace_ghost_notes(
    state: tauri::State<'_, AppState>,
    request: ListWorkspaceGhostNotesRequest,
) -> Result<Vec<GhostNote>, String> {
    state
        .research_service
        .list_workspace_ghost_notes(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn review_ghost_note(
    state: tauri::State<'_, AppState>,
    request: ReviewGhostNoteRequest,
) -> Result<GhostNote, String> {
    state
        .research_service
        .review_ghost_note(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn promote_note_to_thesis(
    state: tauri::State<'_, AppState>,
    request: PromoteNoteToThesisRequest,
) -> Result<ResearchNote, String> {
    state
        .research_service
        .promote_note_to_thesis(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn retry_research_jobs(
    state: tauri::State<'_, AppState>,
    request: RetryResearchJobsRequest,
) -> Result<Vec<PipelineJob>, String> {
    state
        .research_service
        .retry_research_jobs(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn list_research_jobs(
    state: tauri::State<'_, AppState>,
    request: ListResearchJobsRequest,
) -> Result<Vec<PipelineJob>, String> {
    state
        .research_service
        .list_jobs(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn get_note_audit_trail(
    state: tauri::State<'_, AppState>,
    request: GetNoteAuditTrailRequest,
) -> Result<NoteAuditTrail, String> {
    state
        .research_service
        .get_note_audit_trail(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn export_research_bundle(
    state: tauri::State<'_, AppState>,
    request: ExportResearchBundleRequest,
) -> Result<ResearchBundleExport, String> {
    state
        .research_service
        .export_research_bundle(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn search_research_notes(
    state: tauri::State<'_, AppState>,
    request: SearchResearchNotesRequest,
) -> Result<Vec<ResearchNote>, String> {
    state
        .research_service
        .search_notes(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn update_research_workspace(
    state: tauri::State<'_, AppState>,
    request: UpdateResearchWorkspaceRequest,
) -> Result<ResearchWorkspace, String> {
    state
        .research_service
        .update_workspace(request)
        .await
        .map_err(|error| error.to_string())
}

#[tauri::command]
pub async fn archive_research_workspace(
    state: tauri::State<'_, AppState>,
    request: ArchiveResearchWorkspaceRequest,
) -> Result<ResearchWorkspace, String> {
    state
        .research_service
        .archive_workspace(request)
        .await
        .map_err(|error| error.to_string())
}
