use std::net::SocketAddr;
use std::path::PathBuf;

use clap::{Parser, Subcommand};

mod diff;
mod protocol;
mod service;

#[derive(Parser)]
#[command(name = "gitmirror", about = "Git mirror gRPC service")]
struct Cli {
    #[command(subcommand)]
    cmd: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Run the gRPC server.
    Serve(ServeArgs),
}

#[derive(clap::Args)]
struct ServeArgs {
    /// Address to bind the gRPC server to.
    #[arg(long, env = "GITMIRROR_ADDR", default_value = "127.0.0.1:9000")]
    addr: SocketAddr,

    /// Base directory holding bare mirror repos, one per DID (<base>/<did>).
    #[arg(long, env = "GITMIRROR_REPO_BASE", default_value = "repos")]
    repo_base: PathBuf,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()),
        )
        .init();

    match Cli::parse().cmd {
        Command::Serve(args) => service::serve(args.addr, args.repo_base).await,
    }
}
