#include "DatabaseManager.h"

#include "TestFramework.h"
#include "TestUtils.h"

#include "FileLogger.h"

class DbTable
{
public:
    enum class Type
    {
        INT,
        VAR_CHAR255
    };

    using Entry = std::pair<std::string, Type>;

    std::string mName;
    std::vector<Entry> mColumns;

    std::string toString(Type type) const
    {
        switch (type)
        {
        case Type::INT:
                return "int";
        case Type::VAR_CHAR255:
            return "varchar(255)";
        default:
            return "varchar(255)";
        }
    }

    std::string getCreateQuery() const
    {
        std::string query = "CREATE TABLE " + mName + " ( ";
        std::size_t count = 0;
        for (const auto& column : mColumns)
        {
            query += column.first + " " + toString(column.second);
            if (count < mColumns.size() - 1)
            {
                query += ", ";
            }
            count++;
        }
        query += "); ";
        return query;
    }
};

TEST_CASE(TestDatabaseManager, "database")
{
    DatabaseManager db_manager;

    const auto db_path = TestUtils::getTestOutputDir() / "test.db";
    db_manager.openDatabase(db_path);

    DbTable table;
    table.mName = "Persons";
    table.mColumns = { 
        {"PersonID", DbTable::Type::INT},
        {"LastName", DbTable::Type::VAR_CHAR255},
        {"FirstName", DbTable::Type::VAR_CHAR255},
        {"Address", DbTable::Type::VAR_CHAR255},
        {"City", DbTable::Type::VAR_CHAR255}
    };

    std::string statement = table.getCreateQuery();
    db_manager.run(statement);

    db_manager.onShutDown();

    if (std::filesystem::exists(db_path))
    {
        std::filesystem::remove(db_path);
    }

}
