use std::{net::SocketAddr, path::PathBuf};

use anyhow::{Context, ensure};
use clap::Parser;

#[derive(Debug, Clone, Parser)]
#[command(version, about = "A small LAN media browser for smart TVs")]
pub struct Config {
    /// Directory exposed as the media library.
    pub media_root: PathBuf,

    /// HTTP listen address.
    #[arg(long, default_value = "0.0.0.0:8080")]
    pub bind: SocketAddr,

    /// ffprobe executable.
    #[arg(long, default_value = "ffprobe")]
    pub ffprobe: PathBuf,

    /// ffmpeg executable used for DASH packaging and transcoding.
    #[arg(long, default_value = "ffmpeg")]
    pub ffmpeg: PathBuf,
}

impl Config {
    pub fn prepare(mut self) -> anyhow::Result<Self> {
        ensure!(self.media_root.is_dir(), "media root is not a directory");
        self.media_root = self
            .media_root
            .canonicalize()
            .context("failed to resolve media root")?;
        Ok(self)
    }
}
