#include "dps/app.hpp"
#include "dps/config.hpp"

#include <exception>
#include <iostream>
#include <string>

int main(int argc, char **argv) {
    std::string config_path = "config/server.conf";
    for (int i = 1; i < argc; ++i) {
        const std::string arg = argv[i];
        if (arg == "--config" && i + 1 < argc) {
            config_path = argv[++i];
        }
    }

    try {
        dps::AppConfig config = dps::load_config(config_path);
        std::clog << "[startup] config_loaded path=" << config_path
                  << " server=" << config.server.host << ":" << config.server.port
                  << " db_host=" << config.database.host
                  << " db_port=" << config.database.port
                  << " db_name=" << config.database.name
                  << " db_user=" << config.database.user
                  << " db_ssl_mode=" << config.database.ssl_mode
                  << std::endl;
        dps::Application app(std::move(config));
        return app.run();
    } catch (const std::exception &ex) {
        std::cerr << "failed to start dps_manager_server: " << ex.what() << "\n";
        return 1;
    }
}
