#include "databaselms.h"

#include <QtSql>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QMessageBox>

int DataBaseLMS::selectUserID(QString type, QString login, QString password)
{
    QString queryStr;

    if(password != QStringLiteral(""))
    {
        queryStr = QString("SELECT users.user_id "
                                        "FROM public.users "
                                        "WHERE login = '%1' AND password = '%2' AND users.type = '%3' ").arg(
                                               login,
                                               password,
                                               type);
    }
    else
    {
        queryStr = QString("SELECT users.user_id "
                                        "FROM public.users "
                                        "WHERE login = '%1' AND users.type = '%2' ").arg(
                                               login,
                                               type);
    }

    return queryExecInt(queryStr);
}

QString DataBaseLMS::selectUserNameByLogin(QString type, QString login)
{
    QString queryStr = QString("SELECT users.name "
                                                  "FROM public.users "
                                                  "WHERE users.login = '%1' AND users.type = '%2' ").arg(
                                                         login,
                                                         type );

    return queryExecString(queryStr);
}

bool DataBaseLMS::selectUserLoggedIn(QString type, int id_user)
{
    QString queryStr = QString("SELECT users.logged_in "
                                                  "FROM public.users "
                                                  "WHERE user_id = %1 AND users.type = '%2' ").arg(
                                                         QString::number(id_user),
                                                         type );

    return queryExecBool(queryStr);
}

bool DataBaseLMS::selectUserArchived(QString type, int id_user)
{
    QString queryStr = QString("SELECT users.archived "
                                                  "FROM public.users "
                                                  "WHERE user_id = %1 AND users.type = '%2' ").arg(
                                                        QString::number(id_user),
                                                        type );

    return queryExecBool(queryStr);
}

int DataBaseLMS::updateUserLoggedIn(QString type, int id_user, bool loggedIn)
{
    QString queryStr = QString("UPDATE public.users "
                                                 "SET logged_in = %1 "
                                                 "WHERE user_id = %2 AND users.type = '%3' "
                                                 "RETURNING users.user_id").arg(
                                                        loggedIn ? "true" : "false",
                                                        QString::number(id_user),
                                                        type);

    return queryExecInt(queryStr);
}

bool DataBaseLMS::updateAllUsersLoggedIn(QString type, bool loggedIn)
{
    QString queryStr = QString("UPDATE public.users "
                                                 "SET logged_in = %1 "
                                                 "WHERE users.type = '%2' ").arg(
                                                        loggedIn ? "true" : "false",
                                                        type);

    return queryExecBool(queryStr);
}

int DataBaseLMS::updateUserArchived(QString type, int id_user, bool archived)
{
    QString queryStr = QString("UPDATE public.users "
                                                 "SET archived = %1 "
                                                 "WHERE user_id = %2 AND users.type = '%3' "
                                                 "RETURNING users.user_id").arg(
                                                        archived ? "true" : "false",
                                                        QString::number(id_user),
                                                        type);

    return queryExecInt(queryStr);
}
