use crate::generations::{Generation, GenerationStore, GenerationSummary};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct GenerationAddRequest {
    pub kind: String,
    pub title: String,
    pub data: String,
    /// Optional input/source (theme, name, level, etc.) for the history list.
    pub source: Option<String>,
}

#[tauri::command]
pub fn generation_add(
    state: tauri::State<'_, crate::llm::AppState>,
    req: GenerationAddRequest,
) -> Result<i64, String> {
    state
        .gen
        .add(&req.kind, &req.title, &req.data, req.source.as_deref())
        .map_err(|e| e.to_string())
}

#[tauri::command]
pub fn generation_list(
    state: tauri::State<'_, crate::llm::AppState>,
    kind: Option<String>,
) -> Result<Vec<GenerationSummary>, String> {
    state.gen.list(kind.as_deref()).map_err(|e| e.to_string())
}

#[tauri::command]
pub fn generation_get(
    state: tauri::State<'_, crate::llm::AppState>,
    id: i64,
) -> Result<Option<Generation>, String> {
    state.gen.get(id).map_err(|e| e.to_string())
}

#[tauri::command]
pub fn generation_delete(
    state: tauri::State<'_, crate::llm::AppState>,
    id: Option<i64>,
) -> Result<usize, String> {
    state.gen.delete(id).map_err(|e| e.to_string())
}

#[tauri::command]
pub fn generation_counts(
    state: tauri::State<'_, crate::llm::AppState>,
) -> Result<Vec<(String, i64)>, String> {
    state.gen.counts().map_err(|e| e.to_string())
}

// Reference `GenerationStore` so the type is considered used by the module
// (the public functions go through `state.gen`, which already uses it,
// but this silences a future-proofing warning if all wrappers get removed).
#[allow(dead_code)]
fn _store_type_used(_: &GenerationStore) {}
