use serde::{Deserialize, Serialize};

use super::downbeats::DownbeatsState;

/// Response from the /api/gen/{clip_id}/midi endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Midi {
    /// State of the MIDI transcription: "running", "error", "complete", or "streaming"
    pub state: DownbeatsState,

    /// Error message if state is "error"
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_message: Option<String>,

    /// Whether this is the final result
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#final: Option<bool>,

    /// List of transcribed MIDI instruments
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instruments: Option<Vec<MidiInstrument>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MidiInstrument {
    /// Name of the instrument
    pub name: String,

    /// MIDI notes for this instrument
    pub notes: Vec<MidiNote>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MidiNote {
    /// MIDI pitch number (0-127)
    pub pitch: i32,

    /// Start time in seconds
    pub start: f64,

    /// End time in seconds
    pub end: f64,

    /// Note velocity (0.0-1.0)
    pub velocity: f64,
}
