use teloxide::prelude::*;
use vproxy_bot::{
    ui::schema::schema,
    consts::BOT,
    ui::self,
    init_envs,
    redis_storage::create_redis_storage
};


fn main() {
    tokio::runtime::Builder::new_multi_thread()
    .worker_threads(4)
    .thread_stack_size(8 * 1024 * 1024)
    .enable_all()
    .build()
    .unwrap()
    .block_on(async {
        dotenv::dotenv().ok();
        pretty_env_logger::init();

        #[cfg(debug_assertions)]
        log::info!("Service running in debug mode....");
        #[cfg(not(debug_assertions))]
        log::info!("Service running in production mode....");

        init_envs().await;

        log::info!("Preloading translations...");
        if let Err(e) = vproxy_bot::util::i18n::preload_all_translations().await {
            log::error!("Failed to preload translations: {}", e);
            std::process::exit(1);
        }

        log::info!("Creating Redis storage...");

        let master_storage = match create_redis_storage::<ui::MasterState>("MASTER_REDIS_URL").await {
            Ok(storage) => storage,
            Err(e) => {
                log::error!("Failed to create master Redis storage: {}", e);
                std::process::exit(1);
            }
        };
        
        let get_proxy_storage = match create_redis_storage::<ui::GetProxyStates>("GET_PROXY_REDIS_URL").await {
            Ok(storage) => storage,
            Err(e) => {
                log::error!("Failed to create get_proxy Redis storage: {}", e);
                std::process::exit(1);
            }
        };
        
        let purchase_storage = match create_redis_storage::<ui::PurchaseState>("PURCHASE_REDIS_URL").await {
            Ok(storage) => storage,
            Err(e) => {
                log::error!("Failed to create purchase Redis storage: {}", e);
                std::process::exit(1);
            }
        };

        log::info!("Starting bot...");
        Dispatcher::builder(BOT.clone(), schema())
            .dependencies(dptree::deps![
                master_storage,
                get_proxy_storage,
                purchase_storage
            ])
            .enable_ctrlc_handler()
            .build()
            .dispatch()
            .await;
    });
}
