#include "databaselms.h"

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


QList<Instructor> DataBaseLMS::selectAllInstructors()
{
    QList<Instructor> listInstructors;

    QString queryStr = QString("SELECT user_id, name, login, password, is_admin, archived, logged_in "
                                                 "FROM public.users "
                                                 "WHERE users.type = '%1' "
                                                 "ORDER BY user_id ASC").arg(
                                                    TypeUserDBInstructor);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        while (query.next())
        {//Инструктор
            Instructor instructor;
            instructor.setID(query.value(0).toInt());
            instructor.setName(query.value(1).toString());
            instructor.setLogin(query.value(2).toString());
            instructor.setPassword(query.value(3).toString());
            instructor.setIsAdmin(query.value(4).toBool());
            instructor.setArchived(query.value(5).toBool());
            instructor.setLoggedIn(query.value(6).toBool());

            listInstructors.append(instructor);
        }
    }

    return listInstructors;
}

Instructor DataBaseLMS::selectInstructor(int id_instructor)
{
    Instructor instructor;

    QString queryStr = QString("SELECT user_id, name, login, password, is_admin, archived, logged_in "
                                                 "FROM public.users "
                                                 "WHERE users.user_id = %1 AND users.type = '%2' ").arg(
                                                     QString::number(id_instructor),
                                                     TypeUserDBInstructor);

    QSqlQuery query = QSqlQuery(*db);

    if(queryExec(queryStr, &query))
    {
        if (query.first())
        {//Инструктор
            instructor.setID(query.value(0).toInt());
            instructor.setName(query.value(1).toString());
            instructor.setLogin(query.value(2).toString());
            instructor.setPassword(query.value(3).toString());
            instructor.setIsAdmin(query.value(4).toBool());
            instructor.setArchived(query.value(5).toBool());
            instructor.setLoggedIn(query.value(6).toBool());
        }
    }

    return instructor;
}

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

    return queryExecBool(queryStr);
}

int DataBaseLMS::insertInstructor()
{
    QString queryStr = QString("INSERT INTO public.users (type, name, login, password, is_admin, archived, logged_in)"
                                                 "VALUES ('%1', DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT)"
                                                 "RETURNING user_id").arg(
                                                        TypeUserDBInstructor);

    return queryExecInt(queryStr);
}

int DataBaseLMS::insertInstructor(Instructor instructor)
{
    QString queryStr = QString("INSERT INTO public.users (type, name, login, password, is_admin, archived, logged_in) "
                                                 "VALUES ('%1', '%2', '%3', '%4', %5, %6, %7) "
                                                 "RETURNING user_id").arg(
                                                        TypeUserDBInstructor,
                                                        instructor.getName(),
                                                        instructor.getLogin(),
                                                        instructor.getPassword(),
                                                        instructor.getIsAdmin() ? "true" : "false",
                                                        instructor.getArchived() ? "true" : "false",
                                                        instructor.getLoggedIn() ? "true" : "false");

    return queryExecInt(queryStr);
}

int DataBaseLMS::deleteInstructor(int id_instructor)
{
    QString queryStr = QString("DELETE FROM public.users "
                                                 "WHERE user_id = %1 AND users.type = '%2' "
                                                 "RETURNING users.user_id").arg(
                                                        QString::number(id_instructor),
                                                        TypeUserDBInstructor);

    return queryExecInt(queryStr);
}

int DataBaseLMS::updateInstructor(Instructor instructor)
{
    QString queryStr = QString("UPDATE public.users "
                                                 "SET name = '%1', login = '%2', password = '%3', is_admin = %4, archived = %5, logged_in = %6 "
                                                 "WHERE user_id = %7 AND users.type = '%8' "
                                                 "RETURNING users.user_id").arg(
                                                        instructor.getName(),
                                                        instructor.getLogin(),
                                                        instructor.getPassword(),
                                                        instructor.getIsAdmin() ? "true" : "false",
                                                        instructor.getArchived() ? "true" : "false",
                                                        instructor.getLoggedIn() ? "true" : "false",
                                                        QString::number(instructor.getID()),
                                                        TypeUserDBInstructor);

    return queryExecInt(queryStr);
}
