Documentation
use super::*;

pub mod miner;
pub mod ping;
pub mod pool;
pub mod probe;
pub mod proxy;
pub mod router;
pub mod server;
pub mod sync;
pub mod wallet;

#[derive(Debug, Parser)]
pub(crate) enum Subcommand {
    #[command(about = "Run a toy miner")]
    Miner(miner::Miner),
    #[command(about = "Measure Stratum message ping")]
    Ping(ping::Ping),
    #[command(about = "Run a toy solo pool")]
    Pool(pool::Pool),
    #[command(about = "Probe a stratum server")]
    Probe(probe::Probe),
    #[command(about = "Run a toy stratum proxy")]
    Proxy(proxy::Proxy),
    #[command(about = "Run a toy hashrate router")]
    Router(router::RouterCommand),
    #[command(about = "Run API server")]
    Server(server::Server),
    #[command(about = "Sync shares via HTTP")]
    Sync(sync::Sync),
    #[command(about = "Toy wallet for testing")]
    Wallet(wallet::WalletCommand),
}

impl Subcommand {
    pub(crate) async fn run(
        self,
        cancel_token: CancellationToken,
        logs: Arc<logs::Logs>,
    ) -> Result {
        match self {
            Self::Miner(miner) => miner.run(cancel_token).await,
            Self::Ping(ping) => ping.run(cancel_token).await,
            Self::Pool(pool) => pool.run(cancel_token, logs).await,
            Self::Probe(probe) => probe.run(cancel_token).await,
            Self::Proxy(proxy) => proxy.run(cancel_token, logs).await,
            Self::Router(router) => router.run(cancel_token, logs).await,
            Self::Server(server) => {
                let handle = Handle::new();

                let mut sync_task = None;

                if let Some(sync_endpoint) = server.config.sync_endpoint() {
                    let hostname = System::host_name().ok_or(anyhow!("no hostname found"))?;

                    if !sync_endpoint.contains(&hostname) {
                        let mut sync = sync::Sync::default().with_endpoint(sync_endpoint.clone());

                        if let Some(token) = server.config.admin_token() {
                            sync = sync.with_admin_token(token);
                        }

                        let sync_cancel_token = cancel_token.clone();
                        let send_task = tokio::spawn(async move {
                            if let Err(e) = sync.run(sync_cancel_token).await {
                                error!("SyncSend failed: {}", e);
                            }
                        });
                        sync_task = Some(send_task);
                        info!("Started sync to endpoint: {sync_endpoint}");
                    }
                }

                let server_result = server.run(handle, cancel_token).await;

                if let Some(task) = sync_task {
                    info!("Waiting for sync task to finish...");
                    let _ = task.await;
                    info!("Sync task finished");
                }

                server_result
            }
            Self::Sync(sync) => sync.run(cancel_token).await,
            Self::Wallet(wallet) => wallet.run().await,
        }
    }
}