#include <eedb/db/config.hpp>

#include <Wt/WEnvironment>
#include <Wt/WServer>

namespace eedb::db {
PgConfig::PgConfig(const Wt::WEnvironment & env) {
    auto readString = [&env](auto && name, auto & val, auto def) {
        if(!env.server()->readConfigurationProperty(name, val)) {
            val = def;
        }
    };

    auto readInt = [&env](auto && name, auto & val, auto def) {
        std::string tmp;
        if(env.server()->readConfigurationProperty(name, tmp)) {
            val = std::atoi(tmp.c_str());
        } else {
            val = def;
        }
    };

    auto readBool = [&env](auto && name, auto & val, auto def) {
        std::string tmp;
        if(env.server()->readConfigurationProperty(name, tmp)) {
            val = tmp == "true";
        } else {
            val = def;
        }
    };

    readString("db_host", host, "localhost");
    readString("db_user", user, "postgres");
    readString("db_password", password, "postgres");
    readString("db_name", dbname, "eedb");
    readInt("db_port", port, 5432);
    readBool("db_debug", debug, true);
}
}
