#include <eedb/data/PgUser.hpp>

#include <eedb/db/connection.hpp>

#include <sqlpp11/sqlpp11.h>

#include <eedb/model/auth_identity.h>
#include <eedb/model/auth_info.h>
#include <eedb/model/auth_token.h>
#include <eedb/model/user.h>
#include <eedb/model/user_audit.h>

#include <Wt/WString>

#include <boost/spirit/home/support/char_class.hpp>

namespace {
SQLPP_ALIAS_PROVIDER(fk_auth_info_id);
SQLPP_ALIAS_PROVIDER(user_status);

constexpr eedb::user t_user;
constexpr eedb::auth_identity t_identity;
constexpr eedb::auth_info t_info;
constexpr eedb::auth_token authToken;

auto auth = t_user.join(t_info).on(t_user.uid == t_info.user_uid).join(t_identity).on(t_info.id == t_identity.auth_info_id);

auto auth_info_identity = t_info.join(t_identity).on(t_info.id == t_identity.auth_info_id);
auto user_auth_info     = t_user.join(t_info).on(t_info.user_uid == t_user.uid);
auto auth_token_info    = user_auth_info.join(authToken).on(authToken.auth_info_id == t_info.id);
}

namespace eedb {

PgUserIdentity::PgUserIdentity(db::PgConnection & con, int uid, string provider)
 : _db(con), _provider(std::move(provider)), _email("") {
    select(all_of(t_identity)) //
      .from(auth_info_identity)  //
      .where(t_info.user_uid == uid);
}

AuthIdentity::string_view PgUserIdentity::identity() const {
    const auto id_eq       = t_info.user_uid == 0;
    const auto provider_eq = t_identity.provider == _provider;

    auto res = _db(select(t_identity.identity) //
                     .from(auth_info_identity)   //
                     .where(id_eq and provider_eq));

    if(res.empty())
        return {};

//    _name = res.front().identity;

    return _name;
}


AuthIdentity::string_view PgUserIdentity::provider() const {}

const AuthIdentity & PgUser::identity() const {}

const UserConfig & PgUser::config() const {}

UserConfig & PgUser::config() {}

void PgUser::changePassword(UserPassword & password) {}

void PgUser::login(UserPassword & password) {}

void PgUser::logout() {}
}
