use std::path::PathBuf;

use clap::Parser;

/// Cut three synchronized MCAP recordings (ego + both wrists) into merged,
/// per-segment clips defined by a `.mix.json` edit list. Each output MCAP is
/// written in mcap-optimiser byte order (single-channel chunks grouped by
/// channel, MessageIndex relocated into one block before the summary) with
/// UNCOMPRESSED chunks — the payloads are already compressed.
#[derive(Parser, Debug)]
#[command(name = "cutmerge", version, about, long_about = None)]
pub struct Args {
    /// Ego (head) MCAP. Its summary start time anchors the JSON timestamps.
    #[arg(long)]
    pub ego: PathBuf,

    /// Left wrist MCAP. `/camera/<x>` topics are remapped to `/wrist_left/<x>`.
    #[arg(long = "wrist-left")]
    pub wrist_left: PathBuf,

    /// Right wrist MCAP. `/camera/<x>` topics are remapped to `/wrist_right/<x>`.
    #[arg(long = "wrist-right")]
    pub wrist_right: PathBuf,

    /// Segment edit list (`*.mix.json`): array of {index,start_ts,end_ts,...},
    /// timestamps in milliseconds from the recording start.
    #[arg(long)]
    pub json: PathBuf,

    /// Output directory; one `<NNN>.mcap` is written per segment.
    #[arg(long, short = 'o')]
    pub outdir: PathBuf,

    /// Target uncompressed chunk size in bytes (single-channel chunks).
    #[arg(long, default_value_t = 8 * 1024 * 1024)]
    pub chunk_size: u64,

    /// Merge two temporally-adjacent segments into one clip when the gap between
    /// them is strictly smaller than this many milliseconds (and the merged clip
    /// stays within --max-merged-ms). Merging is greedy and chains across a run
    /// of close segments. Set to 0 to disable merging. Default 60000 (1 minute).
    #[arg(long, default_value_t = 60_000)]
    pub merge_gap_ms: u64,

    /// Upper bound, in milliseconds, on a merged clip's duration: a merge that
    /// would make the clip longer than this is not performed, so the segments
    /// stay separate. Default 300000 (5 minutes).
    #[arg(long, default_value_t = 300_000)]
    pub max_merged_ms: u64,

    /// `library` string stamped into the output MCAP header.
    #[arg(long, default_value = "slimcap/0.1.3+h264")]
    pub library: String,

    /// CRF for re-encoding the leading partial GOP at a cut boundary.
    #[arg(long, default_value_t = 20)]
    pub crf: u32,

    /// x264 preset for the leading-GOP re-encode.
    #[arg(long, default_value = "medium")]
    pub preset: String,

    /// Only process the segment with this index (for dev iteration).
    #[arg(long)]
    pub only_segment: Option<u32>,

    /// Maximum tolerated coverage difference between the three inputs, in
    /// milliseconds. Every output is clamped to the time span all three
    /// recordings share, so each clip carries ego + both wrists; when their
    /// first/last message times differ by more than this, the cut is based on
    /// the shortest stream (latest start / earliest end). Boundaries that agree
    /// within this tolerance are left exactly as the edit list asked. Segments
    /// with no shared overlap are skipped.
    #[arg(long, default_value_t = 100)]
    pub max_skew_ms: u64,
}
