use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;

pub mod auto_trigger;
pub mod backend;
pub mod llm;
pub mod prompt;
pub mod recovery;
pub mod render;
pub mod worker;

/// Single-document-per-case filenames. A re-analysis overwrites the
/// document in place (handler deletes it first; worker writes the fresh one).
pub const DOCUMENT_FILE: &str = "document.md";
pub const ANALYSIS_INPUT_FILE: &str = "analysis_input.json";

/// A single request to analyze a case. Payload is tiny (a path), so the
/// channel is unbounded — persistence lives in `analysis_input.json` on
/// disk, not in the queue. Recovery re-enqueues any inputs the server may
/// have held at crash time.
#[derive(Debug, Clone)]
pub struct AnalyzeJob {
    pub case_dir: PathBuf,
}

pub type AnalyzeSender = mpsc::UnboundedSender<AnalyzeJob>;
pub type AnalyzeReceiver = mpsc::UnboundedReceiver<AnalyzeJob>;

pub fn channel() -> (AnalyzeSender, AnalyzeReceiver) {
    mpsc::unbounded_channel()
}

/// Persistent input for a single analysis run. Written by the analyze
/// handler, consumed by the analyze worker, removed after a successful
/// document write.
#[derive(Debug, Serialize, Deserialize)]
pub struct AnalysisInput {
    /// Latest filesystem mtime (server clock) of any `.m4a` in the case at
    /// the moment the analyze button was clicked. Carried through for
    /// diagnostics / future "Nachtrag" detection.
    pub last_recording_mtime: String,
    pub recordings: Vec<RecordingInput>,
    /// Selected LLM backend id; empty string falls back to the default
    /// backend. Old inputs without the field deserialize to `""`.
    #[serde(default)]
    pub backend_id: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RecordingInput {
    /// Recording timestamp from the watch (filename-derived, ISO-8601).
    pub recorded_at: String,
    pub text: String,
}
